4

How to make a PUT request using HttpURLConnection with query parameters?

I am trying to consume a third party REST API using HttpURLConnection but when I try to pass the parameters in the URL, it doesn't work and throw an error as shown below:

The REST API Url could not be found in the mappings registry

This is the code block that doesn't work for me as of now:

    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("&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)
    {
    }

When I provide these parameters in the Body as form-data parameters, the REST API seems provide the response. My question here is that how do I make this work with HttpURLConnection?

What have I tried till now? I have tried to modify the above to something like below, but it doesn't work.

    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)
    {
    }

EDIT: It throws an error with response code as 500

EDIT: 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).

Any pointers or suggestions on this are very much appreciated.

3
  • Never catch exceptions without handle it, atleast log them Commented Apr 26, 2018 at 5:55
  • Yeah, I have implemented in my code but to give a picture of the actual code block added this empty catch block Commented Apr 26, 2018 at 5:56
  • Info for anyone else getting "The REST API Url could not be found in the mappings registry": this error is reported by OpenText Content Server (OTCS) when you use the wrong verb for an endpoint (e.g. a POST for an endpoint that expects only a GET) Commented Jan 20, 2022 at 8:22

2 Answers 2

1

You talk about 'query parameters' and 'parameters in the URL', but neither of the approaches you show does any such things. Both your approaches (try to) send parameters in the request body, aka 'entity', not in the URL. Although body contents may be involved in an application-level query, they are NOT query string aka query parameters at the HTTP level. You also ask 'how do I make this work with HttpURLConnection' as if that were a change or difference when both your attempts already use it.

Your first attempt looks almost correct. It should work if you .setRequestProperty("Content-type", "application/x-www-form-urlencoded") (which is not automatic) and your values either are URLencoded or don't need it (no reserved characters) (depending on the server it may be enough to have no ampersand or equalsign)

Your second attempt also is fairly close. You need to write a boundary before the first part as well, and for each part after Content-disposition: form-data; name="blah" you need one CRLF to end that header line and a second CRLF to end the header block. (MIME multipart format allows multiple header lines in general, although in this case only one is needed.) And the end boundary should be followed by a CRLF (after the extra --).

Both only if you have the URL correct, of course. Nothing will work without the correct URL.

Sign up to request clarification or add additional context in comments.

2 Comments

If I understand your comments right, then both the approaches that I've put in the question are sending the parameters in the request body but not in the URL? (At least the first one I thought was sending these parameters in the URL itself)
Nonetheless, both the approaches don't work as of now! I'll try to implement your suggestions!
0

Best Method to Call WebService with HttpUrlConnection PUT Method

  ApiListener apilistener=null;

public void updateWorker()

    {

progressDialog = ProgressDialog.show(myContext, "Message",      
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);

Thread runThread = new Thread(new Runnable() {

    @Override
    public void run() {

        HttpAppRequest http = new HttpAppRequest();


        try {

            JSONObject paramObject = new JSONObject();
            JSONObject dataObject = new JSONObject();

            dataObject.put("id", csId);

         }                       


        paramObject.put("data", dataObject);

            Log.e(AppConstants.TAG, "Param = " + paramObject.toString());

            AppResponse response = http.putJSONData(BASE_URL + "/updateapi", paramObject.toString(), true);

            if (response.getStatusCode() == 200) {

                String csUpdateResult = response.getContentData();
                Log.e(AppConstants.TAG, csUpdateResult);

                JSONObject updateObject = new JSONObject(csUpdateResult);


      Message completeMessage = handler.obtainMessage(1,    updateObject);
                completeMessage.sendToTarget();
            } else {
                handler.sendEmptyMessage(-1);
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            String csMessage = myContext.getResources().getString(R.string.id_network_response_failure);
            Message completeMessage = handler.obtainMessage(0, csMessage);
            completeMessage.sendToTarget();
        }
    }
});

runThread.start();

}


   /*******************************************************************************************************/ Handler Api Response Here
               Handler handler= new Handler() {
@Override
public void handleMessage(Message inputMessage) {

    progressDialog.dismiss();

    if (inputMessage.what == 1) {
        try {

            JSONObject msgObject = (JSONObject) inputMessage.obj;

            if (msgObject.has("result")) {
                JSONObject resultObject = msgObject.getJSONObject("result");

                if (resultObject.has("status")) {
                    String csStatus = resultObject.getString("status");

                    if (csStatus.equalsIgnoreCase("success")) {

                        apilistener.onUpdate(resultObject.getString("msg"));

                    }
                } else {

                    if(resultObject.has("status"))
                    {
                                apilistener.onFailed(resultObject.getString("reason"));
                    }
                }


            }
        } catch (JSONException e) {
            CommonMethods.showMessageBox("", e.getMessage(), myContext);
        }

    } else if (inputMessage.what == 0) {
        String csMessage = (String) inputMessage.obj;
        CommonMethods.showMessageBox("", csMessage, myContext);
    }

}
       };

       //CallBack Listener to parent Activity/Fragment
      //User listener like this
       public interface ApiListener extends EventListener
      {
           void onSuccess(String msg);
           void onFaiulure(String msg);
        }

       public void setListener(ApiListener listener)
     {

           apilistener=listener;
                }


     }

10 Comments

Can you explain this code in more detail? There is not much that I understand about ApiListener, HttpAppRequest!
HttpAppRequest just handle request send and receive and parse response , you can use standard code for these, Listener is callback to activity because this request is made in separate class and thread. It just send callback to activity/fragment, For examle when apilistener.onSuccess(msg) called then activity can receive msg from this class
Doesn't seem to fit into the scenario well, needs a lot of rework. Can you suggest what changes could be made in the code snippet that I've posted now?
I think you've completely mistaken, I'm not trying to upload a file but trying to send just two parameters. Please cross-check the question once!
And you haven't answered it. You've used another API and you've gratuitously introduced JSON. Doesn't answer the question in any way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.