简体   繁体   中英

Restlet clientResource post returns http error 415

I'm trying to post a JSON to some REST service, but I always end up with a HTTP Error 415: Unsupported Media Type .

The REST Documentation clearly notes I should use application/json , which I do. Surely I must be overlooking something.

public JSONObject fetchResponse() throws ResourceException, JSONException, IOException {
    JRRequest jr = new JRRequest();

    jr.setJql(jql);
    jr.setMaxResults(Integer.parseInt(maxresults));
    jr.setFields(fields);

    Gson json = new Gson();
    String payload = json.toJson(jr);


    JSONObject jsObj = new JSONObject(getClientResource(restUri).post(payload,MediaType.APPLICATION_JSON).getText());

    return jsObj;
}


private ClientResource getClientResource(String uri) {
    ClientResource clientResource = new ClientResource(uri);
    Application app = new Application();
    clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
    return clientResource;
}

Okay, I found the solution. Instead of doing it all in one line, I tried this:

    Representation rep = new StringRepresentation(payload, MediaType.APPLICATION_JSON);
    JSONObject jsObj = new JSONObject(getClientResource(restUri).post(rep).getText());

And it works now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM