-1

I need to send parameter in curl request but i don't about curl request.

Request is: How to send it in java.

curl --user xxx:xxxpass-H "Content-Type: application/json" -X POST -d
'{"ImageId":"local",
  "ImageUrl":"http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg"}'

http://000.00.00.00:8080/api
8
  • 4
    Cann you tell us why you want to use cURL in Java? There are thousands of libraries out there. Commented Mar 10, 2016 at 6:20
  • i dont know .this is third party api i need to just pass few parameters Commented Mar 10, 2016 at 6:25
  • i have developed my web app in java Commented Mar 10, 2016 at 6:26
  • Then take a look at stackoverflow.com/q/24053634/3621175 Commented Mar 10, 2016 at 6:27
  • can you please share code here with my curl request. because i am not getting how to implement my request with this example Commented Mar 10, 2016 at 6:36

2 Answers 2

2

If I understand you correctly, you want to consume the API and somebody gave you the curl command as an example how to do so.

You now want to achieve the same in your JAVA program.

There are a few libraries which allow you to achieve this. Google for «java http client post».

Sending HTTP POST Request In Java or HTTP POST using JSON in Java answers this is as well.

In your case this would be something like:

JSONObject json = new JSONObject();
json.put("ImageId", "local");    
json.put("ImageUrl", "http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://000.00.00.00:8080/api");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanx you so much i am trying it
2

Well, there are better ways of doing this, but if for some reason you really want to use curl,

Runtime.getRuntime.exec("curl --user xxx:xxxpass-H \"Content-Type: 
    application/json\" -X POST -d
    \'{\"ImageId\":\"local\",
    \"ImageUrl\":\"http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg\"}\'"

Essentially, this Java function runs a command in terminal, so you can execute your request.

That is, for a post or put request. For get, just read it's output.

http://000.00.00.00:8080/api

If you want to send the request without having to install curl executables on the client system, look at Apache HttpClient library. :)

5 Comments

can you please explain little more
i am trying to post the request and please can you share the code how to implement my curl request
MY curl request is: curl --user xxx:xxxpass-H "Content-Type: application/json" -X POST -d '{"ImageId":"local", "ImageUrl":"gapcanada.ca/products/res/thumbimg/…"}' 000.00.00.00:8080/api
@IrshadQureshi Updated with more details and your request.
thank u so much arin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.