For one of our projects, we faced issue where-in -
- We were utilizing Apache HTTP Client 4.5 and doing HTTP Post of JSON Message.
- This was resulting HTTP Response as
- HTTP /1.1 400 Bad Request ...
On analysis, we found that HTTP Post was failing just for Big JSON Files of size greater then 7 MB. So, initially we thought it to be a server side issue.
But after further analysis we found that Unix CURL command was able to successfully POST message to API. Thus, we came to know that something was wrong with Scala (JAVA) client code that was using HTTP Client.
Solution -
- We further updated the code and used java.net.{HttpURLConnection, URL} instead of org.apache.http.client.methods.{HttpPost} and it worked fine for us.
But, we were still buzzed with the problem why HTTPClient is not working. We tried using a proxy to capture HTTP Header and Body. So, as to compare difference between various user agents.
- And, we identified that Content-Length for User-Agent : Apache-HttpClient/4.5 (Java/1.8.0._333) was little less then curl/7.83.1 and Java/1.8.0_333, which for us was 7783713 for prior and 7783726 for later.
- On further analysis, we found that code was using org.apache.http.entity.StringEntity -
- which by default uses ISO 8859-1 encoding (text/plain)
- instead of UTF-8 (application/json)
UTF-8 is a multibyte encoding that represent any Unicode character. ISO 8859-1 is a single byte encoding that represents only first 256 Unicode characters.
- We update our code to specify correct ContentType and it worked fine with HTTPClient as well.
- new StringEntity(string, ContentType.APPLICATION_JSON)
Comments
Post a Comment