groovyhttprequestget-request

Set parameters of GET request with groovy


I want to provide parameters for a GET request/ API call to the AYLIEN text analytics API. I can set the headers for the key and ID as authorization and the call itself works (my usage statistics of the API increase after running the code), but I don't know how to provide the text to analyze as a parameter.

def customScript(){

   def connection = new 
   URL("https://api.aylien.com/api/v1/sentiment").openConnection() as 
   HttpURLConnection
   connection.requestMethod = 'GET'

   // set headers
   connection.setRequestProperty('X-AYLIEN-TextAPI-Application-Key','//mykey')
   connection.setRequestProperty('X-AYLIEN-TextAPI-Application-ID', '//myid')                                                                   

   // get the response code - automatically sends the request
   println connection.responseCode  + ": " + connection.inputStream.text

}

Solution

  • In a GET request, the parameters are sent as part of the URL. For example, if you want to add the parameter id=23, you can change the code to:

    def connection = new 
     URL("https://api.aylien.com/api/v1/sentiment?id=23").openConnection() as 
     HttpURLConnection