Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build postman request for Rails POST method

No matter how I format the raw portion of this request, I cannot avoid the parsing error below.

I have a Rails API with a create method that passes the spec, to illustrate that the controller message is sound:

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'm using Postman to try to POST to it. No matter what I try I'm getting the 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've also tried:

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

Code from create method and strong parameters declaration 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
like image 519
Joe Essey Avatar asked Aug 30 '14 22:08

Joe Essey


People also ask

Can postman receive post requests?

The Postman app has a built-in proxy that can capture HTTP and HTTPS traffic. Here's how it works: The Postman app listens for any calls made by a client app or device using the proxy. The Postman proxy captures the request and forwards it to the server.

What is post request in Postman?

A POST is an HTTP Verb similar to a GET request, this specifies that a client is posting data on the given Endpoint. A POST request is a method that is used when we need to send some additional information inside the body of the request to the server.

How do you run a post method?

To send data using the HTTP POST method, you must include the data in the body of the HTTP POST message and specify the MIME type of the data with a Content-Type header. Below is an example of an HTTP POST request to send JSON data to the server. The size and data type for HTTP POST requests is not limited.


3 Answers

Sorry bit too late to answer this but might help someone else.

All you need to do is - in your request header (in postman or whatever client) add

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
like image 151
Tejas Patel Avatar answered Oct 11 '22 20:10

Tejas Patel


Like @Tejas Patel said it's all about headers. But instead of setting them explicitly you can just:

1.- In the request creation area switch to the body tab. Set raw radio button. In the lower text area input your body:

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

2.- Then in a dropdown list to the rigth choose JSON (application/json) option instead of the default Text option. That will auto-set the required headers. That's it - you can press "Send" button.

like image 34
prograils Avatar answered Oct 11 '22 20:10

prograils


Single quotes (') are not actually the legal string delimiter in JSON: a string must be enclosed in double quotes ("). You can get away with it in the browser, since they are string delimiters in javascript. You can easily replicate this in an irb session

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

In addition, given your spec you should be using the second form i.e.

{
  "power_up": {
    "name": "foo",
    "description": "bar"
   }
}
like image 20
Frederick Cheung Avatar answered Oct 11 '22 21:10

Frederick Cheung