加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何为Rails POST方法构建postman请求

发布时间:2020-12-16 19:32:20 所属栏目:百科 来源:网络整理
导读:无论我如何格式化此请求的原始部分,我都无法避免下面的解析错误. 我有一个带有传递规范的create方法的Rails API,以说明控制器消息是合理的: describe "POST power_up" do let!(:client) { FactoryGirl.create :client,name: "joe",auth_token: "asdfasdfasd
无论我如何格式化此请求的原始部分,我都无法避免下面的解析错误.

我有一个带有传递规范的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

我正在使用Postman尝试POST.无论我尝试什么,我都会收到错误:

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中的create方法和强参数声明中的代码:

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

解决方法

对不起,回答这个问题太晚了,但可能会帮助别人.

您需要做的就是 – 在您的请求标题中(在邮递员或任何客户端)添加

Content-Type =’application / json’

或者,您也可以使用curl(source)进行尝试:

curl -X POST -H“Content-Type:application / json”-d'{“power_up”:{“名字”:“foo”,“description”:“bar”}}’127.0.01:3000 / v1 / power_ups.json

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读