0

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
user207421
  • 289,834
  • 37
  • 266
  • 440
Irshad Qureshi
  • 65
  • 1
  • 2
  • 6
  • 4
    Cann you tell us why you want to use cURL in Java? There are thousands of libraries out there. – PatrickMA Mar 10 '16 at 06:20
  • i dont know .this is third party api i need to just pass few parameters – Irshad Qureshi Mar 10 '16 at 06:25
  • i have developed my web app in java – Irshad Qureshi Mar 10 '16 at 06:26
  • Then take a look at http://stackoverflow.com/q/24053634/3621175 – PatrickMA Mar 10 '16 at 06:27
  • can you please share code here with my curl request. because i am not getting how to implement my request with this example – Irshad Qureshi Mar 10 '16 at 06:36
  • There is no such thing as a 'curl request'. `curl` is capable of sending requests in 23 existing formats according to the *man* page I found, most of which have existing Java implementations. You will have to tell us which format you're interested in. Then we will tell you you're off-topic.. – user207421 Mar 10 '16 at 06:56
  • 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 – Irshad Qureshi Mar 10 '16 at 06:59
  • Hi EJP, this is my curl request i am trying to pass using java. and i dont know about this how to send this request – Irshad Qureshi Mar 10 '16 at 07:01

2 Answers2

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();
}
Community
  • 1
  • 1
close2
  • 308
  • 1
  • 6
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. :)

Lee Han Kyeol
  • 2,019
  • 2
  • 25
  • 40
Aaron Esau
  • 885
  • 3
  • 13
  • 30