如何在httparty表单POST后找到响应代码

dan*_*sun 5 post httparty http-response-codes

我使用Httparty使用以下代码对网站进行了POST:

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

如何查看此提交的响应代码?

Dan*_*and 15

post方法的返回值是一个HTTParty::Response对象,它有一个代码方法.将响应分配给变量并使用它来查看http状态代码:

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