Ruby Restclient different double point or astrophobe and is order important - ruby

i am new to ruby RestClient. i have search many example of this restclient and in docruby. For me is important while using ruby restclient, to get the data very fast.
But some are not answers, this is why i would like to question to you all.
i am working on this ruby restclient example Code:
restClient = RestClient::Request.new(
:method => :get,
:url => url,
:verify_ssl => true, #required using https
:content_type => :json,
:accept => :json,
:headers => {
:Authorization => "Bearer #{token}",
}
)
result = restClient.execute()
My first question is what is different of using double point and astrophobe?
restClient = RestClient::Request.new(
:method => :get,
:method => 'get',
...
)
Second question is, is sequences/order in Code important like first url then method or method then url and so on?
restClient = RestClient::Request.new(
:url => :url,
:method => :get,
...
)
#or
restClient = RestClient::Request.new(
:method => :get,
:url => :url,
...
)
third question is, about accept to put in headers. some put accept and content-type in headers and some not, what is different?
restClient = RestClient::Request.new(
:content_type => 'application/json',
:accept => 'application/json',
#or
:headers => {
'hello-token' => "Bearer #{token}",
'content_type'=> 'application/json',
'ACCEPT' => 'application/json'
}
)

What is different of using double point and astrophobe?
:get is a Symbol, 'get' is a String.
It depends on the implementation of the gem if the gem is able to process both. Because the RestClient documentation uses a Symbol in its examples I would recommend doing the same.
But actually – at least in its current version - it doesn' make a difference because the gem translate the argument internally into a string anyway (see initialize and normalize_method)
is sequences/order in Code important
In theory, a hash is an unordered data structure. Therefore the order should not be important in this case. But keep in mind that Ruby's implementation of a hash is actually preserving the order in which the keys are inserted when iterating the hash.
Accept headers
I didn't find any example in the gem's documentation in which they used the first version. Did you actually try both versions? I would be surprised when both worked. Therefore I suggest using the header: version.

Related

POST JSON data with Curl Multi from ruby

I am using the curb gem to do a Curl Multi post using JSON data. However I am unable to actually get the parameters to get posted and have been unable to figure out how to properly configure the parameters.
urls = [
{
:url => "http://localhost:5000/",
:method => :post,
:headers => {'Accept' => 'application/json', 'Content-Type' => 'application/json'},
:post_fields => {'field1' => 'value1', 'k' => 'j'}
}
]
Curl::Multi.http(urls) do |easy, code, method|
puts "#{easy.body_str.inspect}, #{method.inspect}, #{code.inspect}"
end
=>
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>The browser (or proxy) sent a request that this server could not understand.</p>\n", :post, nil
Do that:
urls = [
{
:url => "http://localhost:5000/",
:method => :post,
:headers => {'Accept' => 'application/json', 'Content-Type' => 'application/json'},
:post_fields => {},
:post_body => {'field1' => 'value1', 'k' => 'j'}.to_json,
}
]
The problem: curb doesn't know that you are sending a JSON data. Curb don't read and interprets the contents of :headers. As you can see here, curb transforms your hash into a string separated by "&", which is the default for a normal (non-json) http data sending (eg.: "field1=value1&k=j"). When the server (Rails) read and interprets the header explicity saying that the data is in JSON format, it tries to decode and the result is the same exception that you get when you do that: JSON.parse("field1=value1&k=j").
To solve this, you need to send "post_fields" as an empty hash, and send your actual data by using "post_body". Also, you need to convert your hash to json manually with to_json.
I don't know if they (the curb project owners) know this problem, but I suggest you to warning them about it.

What is the purpose of stubbing an HTTP request (e.g. using the WebMock gem)?

As a precursor FYI, I'm a budding developer. I'm trying to write a test for an http POST method for a Ruby gem. From what I can understand, when you stub an http response, for instance with the Ruby WebMock gem, you're basically telling it what to post and then artificially telling it what to respond with. For example, here is the code I'm trying to test:
## githubrepo.rb
module Githubrepo
include HTTParty
def self.create(attributes)
post = HTTParty.post(
'https://api.github.com/user/repos',
:headers => {
'User-Agent' => 'Githubrepo',
'Content-Type' => 'application/json',
'Accept' => 'application/json'
},
:basic_auth => {
:username => attributes[:username],
:password => attributes[:password]
},
:body => {
'name' => attributes[:repository],
'description' => attributes[:description]
}.to_json
)
Githubrepo.parse_response_from(post, attributes[:wants_ssh])
end
My RSpec test fails when I write:
Githubrepo.create(:repository => 'test', :username => 'test_user', :password => '1234')
because it makes a real HTTP request. It recommends I do the following instead:
stub_request(:post, "https://test_user:test_password#api.github.com/user/repos").
with(:body => "{\"name\":\"test_repo\",\"description\":null}",
:headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json', 'User-Agent'=>'Githubrepo'}).
to_return(:status => 200, :body => "", :headers => {})
But to me, this seems like it's pointless since it's basically telling what to send and what to respond with. I can edit the URL to say "https://bananas#git-banana.banana" and the header to say Content-type => 'Rumplestilskin' and RSpec is ok with that. How am I supposed to integrate this into testing the functionality of the create method I specified above? Or if anything, can somebody point me to a solid beginner guide or blog to help me with this question? The Ruby gem READMEs seem to assume the user knows a thing or two already about this and I don't.
As Steve mentions in a comment, the point of this type of test is not to test the external API but instead that your code to handle and parse the response is correct.
As discussed in the comments to this question, check out the VCR gem for "recording" API responses to make sure your code processes them correctly: https://github.com/vcr/vcr

Authentication headers using Rest Client Ruby Gem

I have already created a basic authentication key, now I am just trying to utilize it. I have tried a few different variations, but none seem to show Authorization in the request headers.
$auth = 'Basic cmFtZXNoQHVzYW1hLmNvbTpyYW1lc2h1JEBtcA=='
#response = resource.post('Authorization' => $auth)
nor
#response = resource.post(:authorization => $auth)
nor
#response = resource.post(:Authorization => $auth)
nor
#response = resource.post(:content_type => :json, :accept => :json, :headers => { 'Authorization:' => $auth })
Unfortunately I am not finding a lot of info in the rdoc that can help me solve this. Does anyone have experience adding auth headers using the Rest Client gem?
For Basic Auth, you should be able to set the user and password in plaintext when you create the resource:
resource = RestClient::Resource.new( 'http://example.com', 'user', 'password' )
But if you really need to set the header directly per request:
#response = resource.post( request_payload, :Authorization => $auth )
should work. If it does not, then you may have set $auth incorrectly. However, I think you just missed adding the request payload, so it was using the hash you supplied for that required param, and not setting any headers at all.
Here's a complete and working example using get (I don't have a test service available with Basic Auth and POST)
require 'rest-client'
require 'base64'
$auth = 'Basic ' + Base64.encode64( 'user:passwd' ).chomp
$url = 'http://httpbin.org/basic-auth/user/passwd'
#resource = RestClient::Resource.new( $url )
#response = #resource.get( :Authorization => $auth )
# => "{\n \"authenticated\": true,\n \"user\": \"user\"\n}"
Note: Though this works, I recommend you use the first and simplest method of supplying user and password to the constructor unless you have good reason not to.
Even though I didn't have a payload to send I was trying to send one without. This ended up being the cause. So I included:
json_str = ''
#response = resource.post(json_str, :content_type => :json, :accept => :json, :Authorization => $auth)
And this worked.
If you don't want to use RestClient::Resource, you can include basic auth in a request like this:
RestClient::Request.execute method: :get, url: url, user: 'username', password: 'secret'
The trick is not to use the RestClient.get (or .post, .put etc.) methods since all options you pass in there are used as headers.
This worked great for me, in case anyone wants to use username/password
RestClient.post("https://USERNAME:PASSWORD#yoursite.com/something", { some: "payload data" })

Setting Request Headers in Ruby

I have the rest client gem and I am defining a request like this:
url = 'http://someurl'
request = {"data" => data}.to_json
response = RestClient.post(url,request,:content_type => :json, :accept => :json)
However I need to set the HTTP header to something. For example an API key. Which could be done in curl as:
curl -XHEAD -H x-auth-user: myusername -H x-auth-key: mykey "url"
Whats the best way to do this in ruby? Using this gem? Or can I do it manually to have more control.
The third parameter is the headers hash.
You can do what you want by:
response = RestClient.post(
url,
request,
:content_type => :json, :accept => :json, :'x-auth-key' => "mykey")
You can also do this
RestClient::Request.execute(
:method => :get or :post,
:url => your_url,
:headers => {key => value}
)
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 :-)
If PUT isn't allowed we can pass it in the header of POST. Headers in bold. This worked for me:
act_resp = RestClient.post url, req_param, **:content_type => :json, :method => :put**

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').
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'}
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 :-)
In rest-client api docs I see that headers is a Hash and if you want to provide both - headers and params, then you need to use a :params key inside the headers Hash. e.g.
headers = { h1 => v1, h2 => v2, :params => {my params} }
What you really need is URI.encode_www_form() method.
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)

Resources