Check Ruby HTTP response for success
For Net::HTTP
, yes, checking the class of the response object is the way to do it. Using kind_of?
(aliased also as is_a?
) is a bit clearer (but functionally equivalent to using <
):
response.kind_of? Net::HTTPSuccess
Calling value
on response
will also raise a Net::HTTPError
if the status code was not a successful one (what a poorly named method…).
If you can, you may want to consider using a gem instead of Net::HTTP
, as they often offer better APIs and performance. Typhoeus and HTTParty are two good ones, among others.
How to check for successful response from API in rails by just using status code?
I would expect any widely supported gem to use standard HTTP response codes to determine if the HTTP response was a success
. For example:
require 'open-uri' # Rails loads this by default.
res = open('http://example.com')
res.status
=> ["200","OK"]
status.include?'OK'
=>true
status.include?'200'
=> true
So long as you trust the gem code making your request to handle standard HTTP response codes, you should be ok. Here's another example using HTTParty gem
require 'HTTParty'
res = HTTParty.get('https://example.com')
res.success?
=> true
How to get only the response code from a HTTP Request in Ruby
I didn't check if it's possible to do with Net::HTTP, but you can use Curb, which is the Ruby wrapper for curl.
Look at Curl::Easy#http_head
With Net::HTTP you can also use HTTP#head
, which requests headers from the server using the HEAD method.
Information about HTTP's method HEAD:
9.4 HEAD
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.
Generate an HTTP response in Ruby
I would start with FakeWeb and see if that meets your needs. If it doesn't you can probably gut whatever you need out of the internals and create your own solution.
How to check if HTTP request status is 200 OK in Rails
require 'net/http'
uri = URI("https://api.wistia.com/v1/projects/#{project_id}.json?api_password=#{auth_token}")
res = Net::HTTP.get_response(uri)
# Status
puts res.code # => '200'
puts res.message # => 'OK'
puts res.class.name # => 'HTTPOK'
# Body
puts res.body if res.response_body_permitted?
Measure response time for a HTTP request using Ruby
As mentioned in the comments, ruby has a benchmark module
require "benchmark"
time = Benchmark.measure do
#requests
end
Rails: get values of http response
Parse the response content with JSON.parse
:
require 'json'
...
data = JSON.parse(response.body)
data['button']['code'] # => "dfhdsg7f23hjgfs7be7"
How to verify the response from api while writing test with Cucumber
If you are using Capybara
this should work for you:
Then /^I should get a response with status (\d+)$/ do |status|
response = post 'http://example.com/api/abc', body: @params,
headers: { 'Accept' => 'application/json',
'login_token' => @login_token,
'Content-Type' => 'application/json' }
response.status_code.should include(status.to_i)
end
Related Topics
Rails: Serializing Objects in a Database
Using a Ruby Script to Login to a Website via Https
Ruby Koans: Why Convert List of Symbols to Strings
Array.Include? Multiple Values
Rails How to Switch Between Dev and Production Mode
What Are the Main Differences Between Sinatra and Ramaze
Creating an Empty File in Ruby: "Touch" Equivalent
"Ago" Date/Time Functions in Ruby/Rails
Which Ruby Memoize Pattern Does Activesupport::Memoizable Refer To
How to Check from Ruby Whether a Process with a Certain Pid Is Running
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby
An Error Occurred While Installing Debugger-Linecache (1.1.1), and Bundler Cannot Continue
Ruby Singleton Methods with (Class_Eval, Define_Method) VS (Instance_Eval, Define_Method)
Rails 3: Generate Unique Codes (Coupons)
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror