I was trying to POST form data that contained Chinese characters from Google
Translation (a side detail), but I could not see a way to set the 'charset'
of the encoder. As a result questionmarks (%3F 's) were being sent on the
wire according to the (urlencoded) debug from HTTPClient.
The only way I could get it to work was to:
http.request(POST, BINARY) {
// ...
def formDataStr = "text="
+URLEncoder.encode(someChineseCharacters, 'UTF-8')
def stream = new ByteArrayOutputStream()
stream.write( formDataStr.getBytes("UTF-8"))
body= stream
headers.'Content-Type' = "application/x-www-form-urlencoded;
charset=UTF-8"
// ...
}
I had attempted to set the contentType with a charset using the delegate but
a NPE is throw from the encoders, it does not like
URLENC + '; charset=UTF-8'
as that is not a registered encoder
Is there a far simpler way...? Or is a feature request required?
Regards
Wayne
--
View this message in context: http://www.nabble.com/How-to-POST-URLENC-UTF-8-form-using-HTTPBuilder--%280.5.0-SNAPSHOT%29-tp22905558p22905558.html
Sent from the groovy - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
--
View this message in context: http://www.nabble.com/How-to-POST-URLENC-UTF-8-form-using-HTTPBuilder--%280.5.0-SNAPSHOT%29-tp22905558p22905663.html
Try this:
http.encoderRegistry = new EncoderRegistry( charset: 'utf-8' )
Then do request( POST, URLENC ) and send the form data as a map rather
than trying to encode it yourself. The EncoderRegistry class already
has a charset field that it uses for the request data. It defaults to
the platform encoding (MacRoman, in your case) which is why you're
seeing that difficulty. This is already available in the current
version HTTPBuilder that you're using; I forgot it was there :)
Let me know if that works for you.
-Tom