HTTPbuilder utf-8 encoding in Grails

I have a problem using HTTPBuilder in Grails.

The code looks something like this.

def http = new HTTPBuilder("theURL") http.request(method, ContentType.JSON) { uri.path = "theURI" headers.'Authorization' = "OAuth $accessToken" headers.'Accept' = "application/json" headers.'content-type' = "application/json;charset=utf-8" response.success = { resp, json -> result = json } } return result 

Now the JSON answer is with "Cobro N 1234", but I need "Cobro Nยบ 1234"

I tried this with curl and the answer was "Cobro Nยบ 1234", it made me think that the problem was with HTTPBuilder, and not with my API, which responded to the request.

I think this is a problem with response encoding.

+5
source share
4 answers
 http.encoders.charset = Charsets.UTF_8 
+1
source

Try the following:

 headers.'content-type' = "application/JSON;charset=UTF-8" //capitalized 

Try it:

 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") 
0
source

Try adding this after the 'http' declaration:

 http.encoderRegistry = new EncoderRegistry( charset: 'utf-8' ) 
0
source

I could not follow if the content to be sent should be encoded using UTF-8 or if the content you receive should be read using UTF-8.

I had a problem sending content using UTF-8, and that is what I did.

 http.request(Method.PUT) { request -> // set headers here request.entity = new StringEntity(json.toString(), "UTF-8") } 

I convert a net.sf.json.JSONObject to String to pass it as the body of the PUT call. I use StringEntity , and I have not previously set the encoding to StringEntity , but there is a constructor that accepts the encoding. Now when I install "UTF-8", it works.

0
source

All Articles