title: Sending JSON by POST using Restlet results in "400 Bad request" error
body: <p>When trying to send a <em>JSON object</em> to the server using <em>POST</em> method, "400 Bad request" error is returned. Standard Java application (SDK) with Restlet api is used to make this request. </p>
  
  <p>Given that the:</p>
  
  <ul>
  <li>host address is "<strong><a href="http://myhostname.com/api/v1/parts/" rel="nofollow">http://myhostname.com/api/v1/parts/</a></strong>"</li>
  <li>access_key is "8QON4KC7BMAYYBCEX" </li>
  </ul>
  
  <p>here is how the code so far looks like:</p>
  
  <pre><code>ClientResource resource = new ClientResource("http://myhostname.com/api/v1/parts/");
  
          resource.setMethod(Method.POST);
          resource.getReference().addQueryParameter("format", "json");
          resource.getReference().addQueryParameter("access_key", "8QON4KC7BMAYYBCEX");
  
              // create json object and populate it
              JSONObject obj = new JSONObject();
          try {
                 obj.put("partId", "23");
                 obj.put("carId", "34");
                 obj.put("name", "chassis");
                 obj.put("section", "frame");
          } catch (JSONException e1) {// handling of exception}
  
          StringRepresentation stringRep = new StringRepresentation(obj.toString());
          stringRep.setMediaType(MediaType.APPLICATION_JSON);
  
          try {
                 resource.post(stringRep).write(System.out); // exception occurs here
          } catch (Exception e) {// handling of exceptions }
  </code></pre>
  
  <p>Response from server is "400 Bad request". Here is the console output:</p>
  
  <pre><code>Bad Request (400) - BAD REQUEST
      at org.restlet.resource.ClientResource.doError(ClientResource.java:612)
      at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1202)
      at org.restlet.resource.ClientResource.handle(ClientResource.java:1069)
      at org.restlet.resource.ClientResource.handle(ClientResource.java:1044)
      at org.restlet.resource.ClientResource.post(ClientResource.java:1453)
      at tests.RESTTestReceiverPOST.main(RESTTestReceiverPOST.java:39)
  </code></pre>
  
  <p>When using Chrome <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm" rel="nofollow">POSTMAN</a> plugin to send this json object (and it works with POSTMAN), the output of the POST request looks like this:</p>
  
  <pre><code>/api/v1/parts/?format=json&amp;access_key=8QON4KC7BMAYYBCEX HTTP/1.1
  Host: myhostname.com
  Content-Type: application/json
  
  { "partId": "23", "carId": "34", "name": "chassis", "section": "frame" }
  </code></pre>
  
  <p>Any suggestions on what may be wrong in the code? Thanks.</p>