Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request cannot send utf_8 charset when the body is not null #4

Closed
wujie8x8 opened this issue Jun 23, 2016 · 7 comments
Closed

Request cannot send utf_8 charset when the body is not null #4

wujie8x8 opened this issue Jun 23, 2016 · 7 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@wujie8x8
Copy link

Issue Summary

I try to use the sendgrid template and I set the personalization, but one value like this : "Irène" this is word using utf-8 charset. I have set headers
requestHeaders.put("Content-Type", "application/json; charset=utf-8");
request.headers = requestHeaders;
But the return is : org.apache.http.client.HttpResponseException: Unsupported Media Type

I use sendgrid 2.2.2 is ok.

So I check the client.java and find you set the
content_type : "application/json" when the body is not null.

Steps to Reproduce

Could you check the headers if there exists the content_type, don't set the content-type automatically?
or you tell me how to sovle this problem ?

Thank you.

Technical details:

  • java-http-client Version: master (latest commit: [commit number])
  • Node.js Version: 1.7.0_60
@toran414
Copy link

I am having this same type of issue, and if I put apache httpclient on DEBUG I can see it returns "Unrecognized UTF8 Byte at position 254". This seems to be because when creating Entities, the client class doesn't specify a Charset or Character Encoding (https://github.com/sendgrid/java-http-client/blob/master/src/main/java/com/sendgrid/Client.java#L210), so it defaults to ContentType.TEXT_PLAIN wich has a Charset of "ISO-8859-1". The API seems to require "UTF-8", so if I change the Client class to specify that for all StringEntities it works:

-    try {
-      httpPost.setEntity(new StringEntity(request.body));
-      if (request.body != "") {
-        httpPost.setHeader("Content-Type", "application/json");
-      }
-    } catch (IOException ex) {
-      throw ex;
+    httpPost.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
+    if (request.body != "") {
+      httpPost.setHeader("Content-Type", "application/json");

@thinkingserious
Copy link
Contributor

@toran414 would you like put in a pull request? If so, could you please sing our CLA: https://github.com/sendgrid/java-http-client/blob/master/CONTRIBUTING.md#cla

@thinkingserious thinkingserious added type: bug bug in the library status: help wanted requesting help from the community labels Jun 24, 2016
@DanailMinchev
Copy link
Contributor

I was debugging and noticed the same problem with Cyrillic symbols .. just need to replace new StringEntity(request.body) with new StringEntity(request.body, "UTF-8") as @toran414 suggested.

@toran414 could you please create PR
or @thinkingserious could you patch this please?

Thank you

@goldbattle
Copy link

For people looking for a quick fix, you can remove all invalid utf-8 chars from your email string.

String email_content = "<html><body>content here</body></html>";
try {
    email_content = new String(email_content.getBytes("US-ASCII"));
 } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

@DanailMinchev
Copy link
Contributor

@goldbattle I want to use UTF-8 characters, not remove them

@goldbattle
Copy link

For my application it was not needed, it doesn't fix the root cause. Just
thought it would help people if they make do with striping the chars.
On Jun 30, 2016 7:04 AM, "Dan" notifications@github.com wrote:

@goldbattle https://github.com/goldbattle I want to use UTF-8
characters, not remove them


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#4 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ACHp4h7pnd1tCUhvg2woxF4O8S9ow072ks5qQ6KmgaJpZM4I9IqH
.

@thinkingserious
Copy link
Contributor

This issue should be fixed in version 2.3.1 (just released)

Thanks again @DanailMinchev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

5 participants