title: How to send a JSON string with an HTTP request using in Java using HTTPClient
body: <p>This is my first question. I love stackOverflow but I've always been a reader, not a writer. Now here goes nothing:</p>
  
  <p>So I've seen a LOT of different questions like this, believe me, I looked... but none of them seemed to answer my question exactly. I'm designing a test harness to test an API, and it involves sending HTTP requests using HttpClient (in Java). For some of the requests, such as POST requests, a JSON string or an XML string must be sent with the request. (I'm only asking about JSON here, if anyone had the answer of how to do the XML as well, I'd love that, but I'll save it for another question.)</p>
  
  <p>Anyways, I have the following code so far which seems like it SHOULD do what I want... but I'm getting 400 error. I'm going to assume that I've properly created an instance of an HttpClient and an HttpPost, applied appropriate headers, etc... the pertinent part is below:</p>
  
  <pre><code>    JSONObject JSONpayload = new JSONObject();
      JSONpayload.put("quantity", 1);
      JSONpayload.put("sku", "21-53429");
      String JSONstring = JSONpayload.toString();
  
      System.out.println("JSON PAYLOAD BEING SENT: " + JSONstring);
      request.setEntity(new StringEntity(JSONstring));
  
      response = client.execute(request);
  
      System.out.println("SERVER RESPONSE STRING: " + response.toString());
  </code></pre>
  
  <p>And I receive the following output:</p>
  
  <pre><code>JSON PAYLOAD BEING SENT: {"quantity":1,"sku":"21-53429"} 
  SERVER RESPONSE STRING: HTTP/1.1 401 Unauthorized 
  [Date: Thu, 27 Jun 2013 19:57:29 GMT, 
   Server: Mule Core/3.3.1, 
   Set-Cookie: sid=h8jumUyMxMztmB1AHtbvmUzzc9WchbiR9dQahD6Q; Version=1;
   Domain=192.168.235.9; 
   Path=/, 
   http.status: 401, 
   X-MULE_SESSION: rO0ABXNyACNvcmcubXVsZS5zZXNzaW9uLkRlZmF1bHRNdWxlU2Vzc2lvbi7rdtEW7GGKAwAEWgAFdmFsaWRMAA1mbG93Q29uc3RydWN0dAAmTG9yZy9tdWxlL2FwaS9jb25zdHJ1Y3QvRmxvd0NvbnN0cnVjdDtMAAJpZHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wAD3NlY3VyaXR5Q29udGV4dHQAJ0xvcmcvbXVsZS9hcGkvc2VjdXJpdHkvU2VjdXJpdHlDb250ZXh0O3hwAXB0ACRjYzQwMmMwNy1kZjYzLTExZTItOGY1Yi1kOTk2MTM2MzZkZjJwc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW10AA9MamF2YS91dGlsL01hcDtMAAVtdXRleHQAEkxqYXZhL2xhbmcvT2JqZWN0O3hwc3IAJG9yZy5tdWxlLnV0aWwuQ2FzZUluc2Vuc2l0aXZlSGFzaE1hcJ3R2e9nRc4AAwAAeHB3DD9AAAAAAAAQAAAAB3QABWR1bW15dAAYOU9WX0FBQUJHQmdBQUFFX0lTQVFBNGMydAAJUm91dGVkVVJMdABhaHR0cDovLzE5Mi4xNjguMjM1Ljk6MzA4MC9nc2kvcmVzdC9XRlMvVE1TTkEtVE1TVVMtU2l0ZS9yZXN0L2NhcnRzLzlPVl9BQUFCR0JnQUFBRV9JU0FRQTRjMi9pdGVtc3QAC0NvbnRleHRQYXRodAATZ3NpLWV3cy1zZXJ2aWNlL3N2Y3QAC291dHB1dC10eXBldAAQYXBwbGljYXRpb24vanNvbnQAClJlcXVlc3RVUkx0AEsvZ3NpLWV3cy1zZXJ2aWNlL3N2Yy92MS4wL3N0b3Jlcy9UTVNVUy9jYXJ0cy85T1ZfQUFBQkdCZ0FBQUVfSVNBUUE0YzIvaXRlbXN0AAdTdG9yZUlEdAAMc3RvcmVzL1RNU1VTdAAKaW5wdXQtdHlwZXEAfgATeHEAfgAJeHg=, 
   X-MULE_ENCODING: UTF-8, 
   Content-Type: text/plain, 
   Transfer-Encoding: chunked]
  </code></pre>
  
  <p>Now, I know the basic things and I'm pretty sure I've taken care of them... like setting the headers Accept application/json, Content-Type application/json, etc..</p>
  
  <p>Any advice would be greatly appreciated. I'm new to using HttpClient and I'm a little lost haha. Thanks guys!</p>