how to find response code after httparty form POST

dan-mi-sun picture dan-mi-sun · Jul 31, 2014 · Viewed 7.2k times · Source

I have made a POST to a site using Httparty with the following code:

HTTParty.post("http://example.com", :body => application_hash.to_json, :headers => {'Content-Type' => 'application/json'})

How can I check what the response code was for this submission?

Answer

Dan Garland picture Dan Garland · Jul 31, 2014

The return value of the post method is a HTTParty::Response object, which has a code method. Assign your response to a variable and use that to take a look at the http status code:

response = HTTParty.post("http://example.com", :body => application_hash.to_json, :headers => {'Content-Type' => 'application/json'})
response.code