Search code examples
javajsonrestlet

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;
}

Solution

  • 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!