Forum Discussion

mehdi_sh's avatar
mehdi_sh
New Contributor
6 years ago

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
  • avidCoder's avatar
    6 years ago

    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.