0

I want to write put Curl to java:

curl -X PUT -u username:password http://localhost:80/api/client/include/clientID

Thats what I googled but my problem is that how can I pass the value of client_id and client to put since there is an /include between them. I am a bit confused of how to write a curl. Can any one help me?

public String RestPutClient(String url, int newValue, int newValue2) {
            // example url : http://localhost:80/api/
            DefaultHttpClient httpClient = new DefaultHttpClient();
            StringBuilder result = new StringBuilder();
            try {
                HttpPut putRequest = new HttpPut(url);
                putRequest.addHeader("Content-Type", "application/json");
                putRequest.addHeader("Accept", "application/json");
                JSONObject keyArg = new JSONObject();
                keyArg.put("value1", newValue);
                keyArg.put("value2", newValue2);
                StringEntity input;
                try {
                    input = new StringEntity(keyArg.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return success;
                }
                putRequest.setEntity(input);
                HttpResponse response = httpClient.execute(putRequest);
                if (response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + response.getStatusLine().getStatusCode());
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        (response.getEntity().getContent())));
                String output;
                while ((output = br.readLine()) != null) {
                    result.append(output);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result.toString();
        }
user3409650
  • 505
  • 3
  • 12
  • 25

1 Answers1

0

I assume the parameters you are passing are supposed to represent client and clientID. You simply need to build the URL your passing to HttpPut from your parameters.

If these are your parameters

url = "http://localhost:80/api/";
newValue = "client";
newValue2 = "clientID";

then your HttpPut initialization would look like this

HttpPut putRequest = new HttpPut(url + newValue + "/include/" + newValue2);

Also see: How do I concatenate two strings in Java?

Community
  • 1
  • 1
Mark
  • 16,772
  • 9
  • 42
  • 55