I try to send a request from android emulator to a restful server. But I always get the Error:
415 Unsupported Media Type.
The code of client:
public JSONtest() throws Exception, IOException{
HttpPost request = new HttpPost(AppServerIP);
JSONObject param = new JSONObject();
param.put("name", "weiping");
param.put("password", "123456");
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
String retSrc = EntityUtils.toString(httpResponse.getEntity());
System.out.println(httpResponse.getStatusLine().getReasonPhrase());
}
The code of the server:
public class resource {
@POST
@Path("/trigger")
@Consumes(MediaType.APPLICATION_JSON)
public Response trigger(JSONObject notify) throws Exception{
return Response.status(Response.Status.OK).entity("134124").tag("213q").type(MediaType.APPLICATION_JSON).build();
}
Fixing 415 Unsupported Media Type errors Ensure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.
Http 415 Media Unsupported is responded back only when the content type header you are providing is not supported by the application. With POSTMAN, the Content-type header you are sending is Content type 'multipart/form-data not application/json .
To do this, open Postman and create a new request by selecting New->Request from the top left: Under Headers, select Key = Content-Type: For Value, select application/json: THANKS FOR READING.
The problem is that the server doesn't know the media type of the client's request. Try something like this in the client code:
request.setHeader("Content-Type", "application/json");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With