简体   繁体   中英

HTTP responses in API Rails

This is my first time working with RAILS and developing API. I am done with the code and basically look like this:

In the controller side:

class Api::V1::EnergyController < ApplicationController
    skip_before_filter :verify_authenticity_token

    def index

    end

    def create
        # Loading data in this part

        # Doing some calculations for the loaded data (Loops and ifs)

        #Outputs (organizing the output data) 


        # Render JSON - Output data
        render json: response_hash

    end 

end

I have been watching some tutorials and reading and they always include HTTP responses(in this way: render json:{status: 200, message: "success"}.to_json ). For the error messages as well in the same form. In my code, I don't have any of such HTTP entries/lines of code. However, when the API finishes running it pops up the following message:

HTTP/1.1 200 OK 
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Etag: W/"6e509fca130161acdbef91f0013f7726"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 0d59ea00-b032-42ae-a28a-3fb6e31b5a59
X-Runtime: 28.173758
Server: WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13)
Date: Wed, 27 May 2015 13:02:58 GMT
Content-Length: 1552
Connection: Keep-Alive
Set-Cookie: request_method=POST; path=/

My question is, should I add those HTTP responses in that way? It is that neccesary?

You are not required to explicitly provide a status code with your response unless you need it to be different from the conventions used by Rails.

For example, when you do

render json: { message: 'success' }

Rails will be default set the status code to 200 as you've already seen based on your original question. You can however be explicit about what status code you want returned

A common code block in a create action would be

def create
  @model = SomeModel.new(create_params)
  if @model.save
    render json: @model, status: :created
  else
    render json: { errors: @model.errors.full_messages }, status: :unprocessable_entity
end

Thus, if the model passes validations and is successfully saved to the DB, we respond with a JSON representation of the model and explicitly set the status to :created which is 201 .

If the model fails to save to the DB, we respond with the errors and explicitly set the status to :unprocessable_entity which is 422 .

Hope this helps.

As per coding standards you need to add status code to each response. The way you mentioned is not correct.

Correct way to add status code in responses

render :status => "200", :json => {:message => "success"}.to_json
render :status => "400", :json => {:status => "Unauthorized Access"}.to_json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM