title: How to use HttpURLConnection PUT with query parameters?
body: <p>How to make a PUT request using <code>HttpURLConnection</code> with query parameters?</p>
  
  <p>I am trying to consume a third party REST API using <code>HttpURLConnection</code> but when I try to pass the parameters in the URL, it doesn't work and throw an error as shown below:</p>
  
  <blockquote>
    <p>The REST API Url could not be found in the mappings registry</p>
  </blockquote>
  
  <p>This is the code block that doesn't work for me as of now:</p>
  
  <pre><code>    URL url;
      StringBuffer response = new StringBuffer();
      try
      {
          url = new URL(" http://thirdparty.com/party/api/v2/ksp/12/ks");
          HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();
          httpURL.setDoOutput(true);
          httpURL.setRequestMethod("PUT");
          StringBuilder sbUrl = new StringBuilder("parameter1_id=");
          sbUrl.append(getParameter1Value())
               .append("&amp;parameter2_id=")
               .append(getParameter2Value());
          final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURL.getOutputStream()));
          writer.write(sbUrl.toString());
          writer.flush();
          writer.close();
  
          // throw the exception here in case invocation of web service
          if (httpURL.getResponseCode() != 200)
          {
              // throw exception
          }
          else
          {
              //SUCCESS
          }
      }
      catch (IOException e)
      {
      }
  </code></pre>
  
  <p>When I provide these parameters in the Body as <code>form-data</code> parameters, the REST API seems provide the response.
  My question here is that how do I make this work with HttpURLConnection?</p>
  
  <p>What have I tried till now?
  I have tried to modify the above to something like below, but it doesn't work.</p>
  
  <pre><code>    try
      {
          url = new URL(" http://thirdparty.com/party/api/v2/ksp/12/ks");
          HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();
          httpURL.setDoOutput(true);
          httpURL.setRequestMethod("PUT");
          httpURL.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + "----WebKitFormBoundarydklhfklsdfhlksh");
          dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
  
          dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"parameter1_id\"");
          dataOutputStream.writeBytes("\r\n" + "parameter1Value" +"\r\n");
          dataOutputStream.writeBytes("--" + "----WebKitFormBoundarydklhfklsdfhlksh");
          dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"parameter2_id\"");
          dataOutputStream.writeBytes("\r\n" + "parameter2Value" + "\r\n");
          dataOutputStream.writeBytes("--" + "----WebKitFormBoundarydklhfklsdfhlksh" + "--");
          dataOutputStream.flush();
          dataOutputStream.close();
          urlConnection.connect();
          // throw the exception here in case invocation of web service
          if (httpURL.getResponseCode() != 200)
          {
              // throw exception
          }
          else
          {
              //SUCCESS
          }
      }
      catch (IOException e)
      {
      }
  </code></pre>
  
  <p><strong>EDIT</strong>: It throws an error with response code as 500</p>
  
  <p><strong>EDIT</strong>: Just to clarify, I'm not trying to upload a file but trying to send the parameters inside the BODY (like Query parameters instead of being sent as URL parameters).</p>
  
  <p>Any pointers or suggestions on this are very much appreciated.</p>