Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct syntax for get requests with rest-client

Right now I can make a request as follows:

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"

But there should be some nicer way to do this. I've tried things like:

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>

and variations thereof (strings instead of symbols for keys, including { and }, making the keys lower case, etc.) but none of the combinations I tried seemed to work. What's the correct syntax here?


I tried the first suggestion below. It didn't work. For the record, this works:

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

but this doesn't:

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

(where I've set version = '2.0').

like image 449
Amit Kumar Gupta Avatar asked Dec 04 '22 03:12

Amit Kumar Gupta


2 Answers

You need to specify query strings parameters with the symbol :params. Otherwise they will be used as headers.

Example with 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'}
like image 75
Pafjo Avatar answered Dec 31 '22 15:12

Pafjo


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 :-)

like image 30
Giorgio Robino Avatar answered Dec 31 '22 15:12

Giorgio Robino