简体   繁体   English

如何构建Rails POST方法的邮递员请求

[英]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: 我有一个带有传递规范的create方法的Rails API,以说明控制器消息是合理的:

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. 我正在使用Postman尝试POST。 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: 邮差要求设置:

邮递员请求的屏幕上限

I've also tried: 我也尝试过:

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

Code from create method and strong parameters declaration in power_ups_controller.rb : power_ups_controller.rb create方法和强参数声明中的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

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(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 @Tejas Patel said it's all about headers. 就像@Tejas Patel所说的那样都是关于标题的。 But instead of setting them explicitly you can just: 但不是明确地设置它们,你可以:

  1. In the request creation area switch to the body tab. 在请求创建区域中切换到body选项卡。 Set raw radio button. 设置raw单选按钮。 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. 然后在rigth的下拉列表中选择JSON (application/json)选项而不是默认的Text选项。 That will auto-set the required headers. 这将自动设置所需的标头。 That's it - you can press "Send" button. 就是这样 - 你可以按“发送”按钮。

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中合法的字符串分隔符 :字符串必须用双引号(“)括起来。你可以在浏览器中使用它,因为它们是javascript中的字符串分隔符。你可以很容易地复制它在一个irb会议上

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 ie 另外,根据您的规范,您应该使用第二种形式,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM