Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported media type for REST POST

I have this VO

public class myVO {
    private final String latitude;
    private final String longtitude;
    private final String meterRadius;

    public myVO(String latitude, String longtitude, String meterRadius) {
        this.latitude = latitude;
        this.longtitude = longtitude;
        this.meterRadius = meterRadius;
    }

    public String getLatitude() {
        return latitude;
    }

    public String getLongtitude() {
        return longtitude;
    }

    public String getMeterRadius() {
        return meterRadius;
    }

}

And rest service

@Path("/listrs")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
    Logger logger = LoggerFactory.getLogger(MyResource.class);

    private final AtomicLong counter;

    public MyResource(String template, String defaultName) {
        //this.template = template;
        //this.defaultName = defaultName;
        this.counter = new AtomicLong();
    }

    @GET
    @Timed
    public List<ResultVO> getMyList() {
        List<ResultVO> list = new ArrayList<>();
        list.add(new ResultVO(counter.incrementAndGet(), "dummy text"));
        list.add(new ResultVO(counter.incrementAndGet(), "dummy text1"));
        return list;
    }

    @POST
    @Timed
    @Consumes(MediaType.APPLICATION_JSON)
    public List<ResultVO> getListByLoc(myVO loc) {
        logger.debug(loc.toString());
        return getMyList();
    }
}

Testing it using

curl -i -X POST -d '{"latitude": "2.3433", "longtitude": "23.2233", "meterRadius":"5"}' http://localhost:8080/listrs

Result I'm getting:

curl: (6) Could not resolve host: 2.3433,
curl: (6) Could not resolve host: longtitude
curl: (6) Could not resolve host: 23.2233,
curl: (3) [globbing] unmatched close brace/bracket at pos 14
HTTP/1.1 415 Unsupported Media Type
Date: Sun, 23 Mar 2014 21:23:16 GMT
Content-Type: text/html;charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1350

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Problem accessing /listrs. Reason:
<pre>    Unsupported Media Type</pre></p><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

</body>
</html>

Any idea what I am doing wrong? Im using dropwizard but I dont think it is relevant.

like image 587
StephenNYC Avatar asked Mar 23 '14 21:03

StephenNYC


People also ask

How do I fix unsupported media type?

Fixing 415 Unsupported Media Type errorsEnsure 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.

How do I fix status 415?

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method. This can happen with an invalid Content-Type HTTP header. The correct MIME media type for JSON is application/json .


1 Answers

Add this with curl:

-H "Content-Type: application/json"

Another thing, you are possibly running this from windows pc. So wrap your json with double quote(") instead of single(')

-d "{\"latitude\": \"2.3433\", \"longtitude\": \"23.2233\", \"meterRadius\":\"5\"}"

Notice, I have escaped the inner quotes with \

Finally, use -v param with your curl. It will help you to see what curl is sending to the server.

like image 162
Sabuj Hassan Avatar answered Oct 01 '22 00:10

Sabuj Hassan