cancel
Showing results for 
Search instead for 
Did you mean: 

How to send a API request with authorization Token via a Groovy script?

SOLVED
Highlighted
New Contributor

How to send a API request with authorization Token via a Groovy script?

Hello,

I want to call a REST APi with a authorization token via my groovy script. How may i do this?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Community Leader

Re: How to send a API request with authorization Token via a Groovy script?

Hi, 

 

You can use Java - Unirest to send the API request with authorization token:-

//Considering you have already fetched the Authorization Token
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.HttpResponse;

String accessToken = "Your Authorization Token" String token_appender = "?access_token=" + accessToken; try { HttpResponse<String> response = Unirest.get("API URL" + token_appender) .header("cache-control", "no-cache") .asString(); String responseBody = response.getBody().toString(); System.out.println("********** Response Generated **********"); System.out.println(responseBody); }catch(Exception e){ e.printStackTrace(); return; }

This is the easiest way to do it.

 

If it really helped you out. Please accept as solution and don't forget to give kudos. 

5 REPLIES 5
Community Hero

Re: How to send a API request with authorization Token via a Groovy script?

Just curious why groovy step while REST request step is readily available for the same?


Regards,
Rao.
New Contributor

Re: How to send a API request with authorization Token via a Groovy script?

Hi Rao,

This is because of test automation; we need to verify some condition and in case trigger calling an API with sending some params such as Authentication Token, etc

Community Hero

Re: How to send a API request with authorization Token via a Groovy script?

Of course, most of the people does the automation only.
And everything you mentioned be done using REST Request step.
In case you need any assertion, even that is possible to apply to the step.


Regards,
Rao.
Community Leader

Re: How to send a API request with authorization Token via a Groovy script?

Hi, 

 

You can use Java - Unirest to send the API request with authorization token:-

//Considering you have already fetched the Authorization Token
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.HttpResponse;

String accessToken = "Your Authorization Token" String token_appender = "?access_token=" + accessToken; try { HttpResponse<String> response = Unirest.get("API URL" + token_appender) .header("cache-control", "no-cache") .asString(); String responseBody = response.getBody().toString(); System.out.println("********** Response Generated **********"); System.out.println(responseBody); }catch(Exception e){ e.printStackTrace(); return; }

This is the easiest way to do it.

 

If it really helped you out. Please accept as solution and don't forget to give kudos. 

Community Manager

Re: How to send a API request with authorization Token via a Groovy script?

Hi @mehdi_sh,

 

Did any of the suggestions given here help you? If so, please click the Accept as a Solution button next to the post that helped you.

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Top Kudoed Authors