Rails status 406 POST response

I have been struggling with this issue for the past 4 hours. Whenever I send a POST request with no arguments (body = "{}"), I get a response with a 406 HTTP status error code.

My controller looks as simple as

def resetFixture

    respond_to do |format|
      format.json { render :json=>final_obj, :status=>:ok}
    end
  end

      

Routes

match 'TESTAPI/resetFixture' => 'users#resetFixture', :via => :post

      

I am trying to return a JSON response using "{errCode: 1}"

If I execute a curl request with -d "" I get the response I want, but how can I fix this error code? I seem to have already specified the JSON and status.

It might be more intuitive if I also enabled the debug logs from the server for the fail request:

+3


source to share


1 answer


Since you cannot request resetFixture.json, you need to add the following request to your request:

Accept: application/json

This will tell rails that you want JSON returned.



You can test this with curl by adding the following argument: -H "Accept: application/json"

Also, if you want Rails to always be the default for JSON, you can change the route:

match 'TESTAPI/resetFixture' => 'users#resetFixture', :via => :post, :defaults => { :format => 'json' }

+2


source







All Articles