[groovy-user] How to POST URLENC UTF-8 form using HTTPBuilder (0.5.0-SNAPSHOT)

714 views
Skip to first unread message

wkeenan

unread,
Apr 6, 2009, 5:51:00 AM4/6/09
to us...@groovy.codehaus.org

Hi,

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


wkeenan

unread,
Apr 6, 2009, 5:57:52 AM4/6/09
to us...@groovy.codehaus.org

I should add that I am on Mac and my platform default encoding is MacRoman.

--
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

Tom Nichols

unread,
Apr 13, 2009, 4:33:27 PM4/13/09
to us...@groovy.codehaus.org
I'll have to think about this for a bit. It is certainly more
complicated than it should be. But you're going about it the right
way by adding the charset via the header. Maybe I could add a
convenient way to set the charset that can be respected by the various
encoders. But in any case it should not try to look at the charset
when trying to determine the encoder. (But I'm sure it does right now
:)

Tom Nichols

unread,
Apr 15, 2009, 2:17:39 PM4/15/09
to us...@groovy.codehaus.org
Hi Wayne,

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

Reply all
Reply to author
Forward
0 new messages