Check Ruby Http Response for Success

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



Leave a reply



Submit