How to create a mail request for the POST Rails method

No matter how I format the raw part of this query, I cannot avoid a parsing error.

I have a Rails API with the create method that passes the specification to show that the controller message sounds:

describe "POST power_up" do let!(:client) { FactoryGirl.create :client, name: "joe", auth_token: "asdfasdfasdfasdfasdfasdf" } it "should create and save a new Power Up" do expect { post :create, format: :json, power_up: FactoryGirl.attributes_for(:power_up) }.to change(V1::PowerUp, :count).by(1) end end 

I am using Postman to try POST. No matter what I try, I get an error:

 Started POST "/v1/power_ups.json" for 127.0.0.1 at 2014-08-30 18:05:29 -0400 Error occurred while parsing request parameters. Contents: { 'name': 'foo', 'description': 'bar' } ActionDispatch::ParamsParser::ParseError (795: unexpected token at '{ 'name': 'foo', 'description': 'bar' } 

Postman request setup:

screen cap of postman request

I also tried:

 { 'power_up': { 'name': 'foo', 'description': 'bar' } } 

Code from the method for creating and declaring strong parameters in power_ups_controller.rb :

 def create @power_up = PowerUp.new(power_up_params) if @power_up.save! redirect_to @power_up end end private def power_up_params params.require(:power_up).permit(:name, :description) end 
+8
rest ruby-on-rails postman
source share
3 answers

Sorry it's too late to answer this, but may help someone else.

All you have to do is add in the request header (in the mail carrier or any other client)

Content-Type = 'application/json'

Alternatively, you can also try it with curl (source):

curl -X POST -H "Content-Type: application/json" -d '{"power_up": { "name": "foo", "description": "bar" } }' 127.0.01:3000/v1/power_ups.json

+9
source share

Like @Tejas Patel , he talked all about headlines. But instead of setting them explicitly, you can simply:

  • In the query creation area, click the body tab. Set the raw switch. In the lower text area, enter your body:

    { "power_up": { "name": "foo", "description": "bar" } }

  • Then select " JSON (application/json) " from the drop-down list instead of the default Text option. This will automatically set the desired headers. What is it - you can click the "Send" button.

+6
source share

Single quotes (') are not really legal string separators in JSON: the string must be enclosed in double quotes ("). It can escape with it in the browser, as they are string delimiters in javascript. You can easily reproduce this in an irb session

 JSON.parse(%q[{'foo': 'bar'}]) #=> raises JSON::ParserError JSON.parse(%q[{"foo": "bar"}]) #=> ok 

Also, considering your specification, you should use the second form ie

 { "power_up": { "name": "foo", "description": "bar" } } 
+5
source share

All Articles