0

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;
}
megamoth
  • 695
  • 2
  • 12
  • 27
rdem
  • 195
  • 1
  • 2
  • 12
  • Do you have a `Restlet` server? or are you posting on a different server like `Node` or `PHP`? – megamoth Aug 10 '15 at 11:56
  • Posting on a different server, JIRA actually (Tomcat). – rdem Aug 10 '15 at 12:24
  • I've tried the Advanced REST client in Chrome, posting a json payload with application/json, and it works fine. So the server's definitely working fine. – rdem Aug 10 '15 at 12:43
  • Have you checked if the `JSON` is malformed or did some logs on the request headers? Printing the headers is shown in this link http://stackoverflow.com/questions/17743785/how-to-access-header-values-of-request-in-java-restlet – megamoth Aug 10 '15 at 12:50

1 Answers1

1

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!

rdem
  • 195
  • 1
  • 2
  • 12