4

现在我可以提出如下请求:

user = 'xxx'  
token = 'xxx'  
survey_id = 'xxx'  
response = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getLegacyResponseData&User=#{user}&Token=#{token}&Version=2.0&SurveyID=#{survey_id}&Format=XML"

但是应该有一些更好的方法来做到这一点。我试过这样的事情:

response = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php", :Request => 'getLegacyResponseData', :User => user, :Token => token, :Version => '2.0', :SurveyID => survey_id, :Format => 'XML'</code>

及其变体(字符串而不是键的符号,包括{和},使键小写等),但我尝试的任何组合似乎都不起作用。这里的正确语法是什么?


我尝试了下面的第一个建议。它没有用。作为记录,这有效:

surveys_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getSurveys&User=#{user}&Token=#{token}&Version=#{version}&Format=JSON"

但这不是:

surveys_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php", :params => {:Request => 'getSurveys', :User => user, :Token => token, :Version => version, :Format => 'JSON'}

(我设置了version = '2.0')。

4

4 回答 4

13

您需要使用符号 :params 指定查询字符串参数。否则它们将被用作标题。

带参数的示例:

response = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php", :params => {:Request => 'getLegacyResponseData', :User => user, :Token => token, :Version => '2.0', :SurveyID => survey_id, :Format => 'XML'}
于 2012-07-14T06:37:26.940 回答
3

I had the same problem with Rest-Client (1.7.2) I need to put both params and HTTP headers.

I solved with this syntax:

params = {id: id, device: device, status: status}
headers = {myheader: "giorgio"}

RestClient.put url, params, headers

I hate RestClient :-)

于 2014-11-28T10:56:23.890 回答
1

rest-client api docs中,我看到这headers是一个Hash,如果你想同时提供 - 标头和参数,那么你需要在哈希中使用一个:params键。headers例如

headers = { h1 => v1, h2 => v2, :params => {my params} }

于 2015-07-06T13:05:12.813 回答
0

你真正需要的是URI.encode_www_form()方法。

uri = URI("https://survey.qualtrics.com/WRAPI/ControlPanel/api.php")
request_params = {
  Request: 'getLegacyResponseData',
  ...
}
uri.query = URI.encode_www_form(request_params)
response = RestClient.get(uri.to_s)
于 2018-10-12T09:03:32.813 回答