0
How do I request HTTP in Ruby? I need to implement an API, and for that it is necessary to make a REST request for such a URL, as I do a POST for such a request?
0
How do I request HTTP in Ruby? I need to implement an API, and for that it is necessary to make a REST request for such a URL, as I do a POST for such a request?
1
Available in ruby documentation DOC, the example below
require 'net/http'
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
puts res.body
But then the parameters I pass where, and also how to handle the server response and what is => Uri, 'q' => ['ruby', 'perl'], 'max' => '50'
That 'q' is a parameter, and 'max' is another
Makes sense, the res.body is the server response ?
0
A Gem HTTP (http.Rb) makes this work easier. To install, just add to your Gemfile:
gem "http"
and execute bundle install
in the shell.
require 'http'
response = HTTP.post("https://api.test/post", :params => {:id => 5})
response.body # retorna um objeto representando a resposta
response.code # retorna o código HTTP da resposta, e.g. 404, 500, 200
If you need authentication, simply:
HTTP.basic_auth(:user => "user", :pass => "pass")
.get("https://example.com")
or if you want to pass the authorization "raw":
HTTP.auth("Bearer VGhlIEhUVFAgR2VtLCBST0NLUw")
.get("https://example.com")
Browser other questions tagged ruby rest http
You are not signed in. Login or sign up in order to post.
Search on the Gem httparty.
– Sidney