0

I'm running this below code in my java class and ideally it should return me a bearer token ....but when I run the code I get Exception.

The below is the code I'm running:

String[] command = {"curl","-v","-
X","POST","https://<Domain name>/oauth2/token","-H","cache-control: 
no-cache","-H","content-type: application/x-www-form-urlencoded","-
H","postman-token: a1f5e569-cbc7-63cc-8667-45d85d74784b","-d","client_id=XXXXXXX","&","client_secret=XXXXXXXXXX","&","grant_type=client_credentials"};
for (String s: command)
            System.out.print(s); 
        ProcessBuilder process = new ProcessBuilder(command);
         Process p;
try {

                p = process.start();
     }

The exception I get is

java.io.IOException: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified

Can someone please let me know what was the issue or how to run the curl command from JAVA.

7
  • Is curl on PATH? Commented Jan 26, 2018 at 18:46
  • yes This the actual cURL command ` curl -v -X POST https://<Doman name>/oauth2/token -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' -H 'postman-token: a1f5e569-cbc7-63cc-8667-45d85d74784b' -d 'client_id=XXXX&client_secret=XXXXXX&grant_type=client_crede‌​ntials' ` Commented Jan 26, 2018 at 19:09
  • Are you using Linux or Windows or Mac? Commented Jan 26, 2018 at 19:26
  • I'm using Windows that err is when I ran it in Windows. But I'm using eclipse to run. Also when I run it on linux machine my application is running but no result when the control reaches p = process.start(); I mean its not going to next line @gil.fernandes Commented Jan 26, 2018 at 19:57
  • Could you run this oneliner from Eclipse and see, if curl is indeed in the path: new ProcessBuilder("cmd", "/k", "path").redirectOutput(ProcessBuilder.Redirect.INHERIT).start(); . This prints out the path in your environment. Commented Jan 26, 2018 at 19:59

1 Answer 1

0

Have you tried Rest Assured Framework? I had same requirements sometime back and could achieve it using Rest Assured.

RestAssured.baseURI = CURLURL;
Response r = (Response) given().relaxedHTTPSValidation().contentType("application/json").body(curlRequest)
.when().post("");
return r.getBody().asString();
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry but I couldn't understand can you help me with a little more detailed code

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.