How to build postman request for Rails POST method

前端 未结 3 1605
暗喜
暗喜 2021-01-02 09:55

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 illu

相关标签:
3条回答
  • 2021-01-02 10:08

    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"
       }
    }
    
    0 讨论(0)
  • 2021-01-02 10:30

    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

    0 讨论(0)
  • 2021-01-02 10:30

    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.

    0 讨论(0)
提交回复
热议问题