Reputation: 122
While working on a Micro-Service, I have to hit the REST api of the 3rd party. I am using the Spring Boot Application with Jersey library. Now the problem is that I am getting the content type of the response as "text/html; charset=utf-8".
If I hit the same call using the REST client, I get the right content type as application/json;charset=UTF-8. Why so ?
Below is the Java source code for the same -
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")
@POST
@Path("/endPoint")
@Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")
public JSONObject getAccessToken(@FormParam("item1") String item1,@FormParam("item2") String item2,@FormParam("item3") String item3,@FormParam("item4") String item4) throws Exception {
System.out.println("Enter to test");
String extendedUrl = "?item1="+item1+"&item2="+item2+"&item3="+item3+"&item4="+item4;
JSONObject jObject = null;
try {
jObject = postCall(extendedUrl);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Box Auth Response :: "+jObject.toJSONString());
return jObject;
}
// Short description of the logic to execute the request
public void postCall(String extendedUrl)
{
String url = "baseurl";
url+=extendedUrl;
HttpsURLConnection conn = openConnection(apiUrl);
conn.connect();
status = conn.getResponseCode();
String responseContentType = conn.getContentType();
System.out.println("responseContentType ::"+responseContentType);
}
So when I debug the code, responseContentType comes out as text/html; charset=utf-8. Is there any reason for the same ? How will get this as application/json;charset=UTF-8?
Help would be appreciated.
Upvotes: 2
Views: 2525
Reputation: 122
Actually I was hitting some different end point which was not the part of OAuth in turn I was getting the HTML response. Issue has been solved.Thanks guys.
Upvotes: 0
Reputation: 1860
Check "accept" header of your request
Accept: application/json Content-Type: application/json
Upvotes: 1