2

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

}
mles
  • 4,534
  • 10
  • 54
  • 94
Colle
  • 154
  • 1
  • 11

1 Answers1

1

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
Gal Naor
  • 2,397
  • 14
  • 17
  • Thanks! The problem is that it only works when providing a single word, e.g. def connection = new URL("https://api.aylien.com/api/v1/sentiment?text=good").openConnection() . How can I provide an entire sentence? Putting it in quotation marks doesn't work because of syntax and just typing ("https://api.aylien.com/api/v1/sentiment?text=this is a good example") also doesn't work – Colle Apr 10 '19 at 11:29
  • You need to convert it to utf-8, by doing: `URLEncoder.encode(url, "utf-8")` – Gal Naor Apr 10 '19 at 12:12