如何建立对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.
我有一个Rails API,其create方法可以传递规范,以说明控制器消息是正确的:
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
我正在使用Postman尝试对其进行发布。不管我尝试什么,都会遇到错误:
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'
}
邮递员请求设置:
我也尝试过:
{
'power_up': {
'name': 'foo',
'description': 'bar'
}
}
创建方法和强大的代码 power_ups_controller.rb
中的参数声明:
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
推荐答案
很抱歉,回答这个问题为时已晚,但可能会对其他人有所帮助。
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'
,也可以尝试使用curl(源):
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
这篇关于如何建立对Rails POST方法的邮递员请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!