简体   繁体   中英

Postman post request to Rails API strong params error

I have a controller that uses strong params with require and permit . When I'm testing this endpoint with postman, I keep getting something like:

undefined method `permit' for "{foo: [1,2,3], bar: 1}":String

I know this controller works fine when I make the post request through the UI, but testing with postman gives me this error each time. I'm sending my payload through raw as JSON(application/json) and the header with the corresponding content type too.

This is the body I'm passing thru:

{
    "foos": {"foo":[1,2,3], "bar": 2675}
}

在此处输入图片说明

  def foos_params
    params.require(:foos).permit(:foo, :bar)
  end

In Headers tab add Content-Type: Application/json 在此处输入图片说明

In my opinion,

with Postman input params:

key : value

foos[foo] : [1,2,3] # this is string: JSON.stringify([1,2,3]) => "[1,2,3]"

foos[bar] : 2675

In controller:

def foos_params
   params.require(:foos).permit(:foo, :bar)
end

But, we need convert string to array for :foo :

ActiveSupport::JSON.decode(foos_params[:foo]) # => [1, 2, 3]

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