redouane59 / twittered

Twitter API client for Java developers
Apache License 2.0
235 stars 64 forks source link

characters are corrupted in request payload when default charset encoding is not UTF-8 #379

Open apptaro opened 2 years ago

apptaro commented 2 years ago

When the default charset encoding (file.encoding) is not UTF-8, twitterClient.postTweet("あいうえお") results in a tweet whose characters are corrupted.

This is because twittered uses OAuthRequest.setPayload(String), and ScribeJava uses String.getBytes() to obtain the byte array data of the request payload. To fix this issue, twittered should use OAuthRequest.setPayload(byte[]) and pass the result of String.getBytes(StandardCharset.UTF_8) at the code below:

https://github.com/redouane59/twittered/blob/develop/src/main/java/io/github/redouane59/twitter/helpers/AbstractRequestHelper.java#L76

Note: Json should always be encoded in UTF-8 as defined by RFC 8259.


Just for reference, below is the related code sections in ScribeJava:

com.github.scribejava.core.model.OAuthRequest.setPayload(String payload) https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java#L180

com.github.scribejava.core.oauth.OAuthService.execute(OAuthRequest request) https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuthService.java#L108

com.github.scribejava.core.httpclient.jdk.JDKHttpClient.BodyType.setBody(String body) https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClient.java#L164