Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly check the HTTP response code in Ruby
    primarykey
    data
    text
    <p>I am writing a gem that will serve as a REST API client for the service my company provides.</p> <p>For that, I have a <code>Channel</code> class that will serve as a means of interacting with the <code>/channels</code> path of the API. It uses <a href="https://github.com/jnunemaker/httparty" rel="nofollow"><code>HTTParty</code></a> for the actual HTTP calls. This class has a method called create, that POSTs a Channel instance with the required attributes.</p> <p>This is what the method looks like:</p> <pre><code>def create body = { external_id: external_id, name: name, description: description } action = post "/", body case action.response.class when Net::HTTPAccepted then return action when Net::HTTPNotAcceptable then raise HTTPNotAcceptable else raise "Server responded with " + Net::HTTPResponse::CODE_TO_OBJ[action.response.code].to_s end end </code></pre> <p>The <code>post</code> method is an abstraction that transforms the body hash into JSON and attaches authentication and other attributes to the actual <code>HTTParty</code> call. </p> <p>This does not work. Even when the response is a <code>202</code>, this <code>case</code> statement always falls on the <code>else</code> condition. I have attached a <code>pry</code> session after the <code>post</code> call to verify that the response was correct:</p> <pre><code> 38: def create 39: body = { external_id: external_id, 40: name: name, 41: description: description } 42: action = post "/", body =&gt; 43: case action.response.class 44: when Net::HTTPAccepted then return action 45: when Net::HTTPNotAcceptable then raise HTTPNotAcceptable 46: else raise "Server responded with " + Net::HTTPResponse::CODE_TO_OBJ[action.response.code].to_s 47: end 48: end [1] (pry) #&lt;ZynkApi::Channel&gt;: 0&gt; action.response.class =&gt; Net::HTTPAccepted </code></pre> <p>But it still falls on <code>else</code>:</p> <pre><code>[1] (pry) #&lt;ZynkApi::Channel&gt;: 0&gt; action.response.class =&gt; Net::HTTPAccepted [2] (pry) #&lt;ZynkApi::Channel&gt;: 0&gt; n From: /Users/cassiano/projects/Zynk/src/zynk_api/lib/zynk_api/channel.rb @ line 38 ZynkApi::Channel#create: 38: def create 39: body = { external_id: external_id, 40: name: name, 41: description: description } 42: action = post "/", body 43: case action.response.class 44: when Net::HTTPAccepted then return action 45: when Net::HTTPNotAcceptable then raise HTTPNotAcceptable =&gt; 46: else raise "Server responded with " + Net::HTTPResponse::CODE_TO_OBJ[action.response.code].to_s 47: end 48: end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload