Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsupported media type for an android rest client

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();       
}
like image 408
user769978 Avatar asked May 26 '11 16:05

user769978


People also ask

How do I fix unsupported media type error?

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.

What does media type not supported mean?

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.

What does unsupported media type mean in Postman?

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 .

How do I change media type in Postman?

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.


1 Answers

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");

like image 108
Tom Christie Avatar answered Nov 14 '22 22:11

Tom Christie