用Groovy设置GET请求的参数

时间:2019-04-10 10:34:22

标签: groovy httprequest get-request

我想为对AYLIEN text analytics API的GET请求/ API调用提供参数。我可以将密钥和ID的标头设置为授权,并且调用本身可以正常工作(运行代码后,API的使用统计信息会增加),但是我不知道如何提供要分析的文本作为参数。

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

}

1 个答案:

答案 0 :(得分:1)

在GET请求中,参数作为URL的一部分发送。 例如,如果要添加参数id=23,则可以将代码更改为:

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