Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST Request for Rails API returns ParameterMissing for strong params

Found a couple of posts kinda related, but none of them help me with my issue.

So I created a simple products API using Rails:

class API::V1::ProductsController < ApplicationController
  respond_to :json

  def index
    respond_with Product.all
  end

  def show
    respond_with Product.find(params[:id])
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      respond_with @product
    end
  end

  def update 
    respond_with Product.find(params[:id]).update_attributes(params[:product])
  end

  def destroy
    respond_with Product.destroy(params[:id])
  end

  private
  def product_params
    params.require(:product).permit(:name, :category, :price, :release_date)
  end
end

I'm trying to send a POST request to create a product using RESTClient. when I try to fill in the POST request body with:

{ "product"=> {
    "name": "Acoustic Guitar2",
    "category": "Instrument",
    "price": 600.0,
    "release_date": "2012-01-10"
} }

or

{
    "name": "Acoustic Guitar2",
    "category": "Instrument",
    "price": 600.0,
    "release_date": "2012-01-10"
}

or

{ "product": {
    "name": "Acoustic Guitar2",
    "category": "Instrument",
    "price": 600.0,
    "release_date": "2012-01-10"
} }

I get an error:

ActionController::ParameterMissing at /api/v1/product
param not found: product

From my research, it has something to do with Rail 4's strong parameters, but I don't really know how to fix it. Any ideas? I know there are ruby-api gems out there, but I'd like to learn how it's done manually.

like image 536
Danny Y Avatar asked Mar 21 '14 04:03

Danny Y


1 Answers

The real problem is not with the strong parameters. When you write params.require[:product], that means that your server will throw a ParameterMissing error if it cannot find the product parameter, and here, it cannot find it ! Why ? Cause as you said, you wrote in the body of the the request, you should pass these arguments as form parameters, I never used RESTClient but there is a difference between form parameters and the body in a request.

You should refer to this previous question first : Firefox Add-on RESTclient - How to input POST parameters? Try again, and come back here if it is still not working !

I actually prefer using PostMan (Chrome add on) than REST client, it is easier to use I think.

There are several ways of sending post informations.

The first request look like this

POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product[name]"

foo
----WebKitFormBoundaryE19zNvXGzXaLvS5C

The second one like this

POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache

Content-Type: application/x-www-form-urlencoded

product%5Bname%5D=foo

And the third one like this

POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache

{'product': {'name': 'foo'}}

the content-type or content-disposition is always different, I think it is the reason why it is changing. You can pass JSON to the Rails server but only if your request is correctly configured ! With Postman, I do not have to think about, it, it works all the time :)

The default content type is probably application/x-www-form-urlencoded. When you are using jQuery, it is the default one at least. For this content type, the second request body has to be provided. However, if you precise a json content-type, it should be fine :)

In javascript/jQuery:

$.post('/products', {'product': {'name': 'foo'}}), function(data){/*process response*/}, 'json')

should work properly

like image 170
Uelb Avatar answered Sep 27 '22 18:09

Uelb