发布JSON以通过API

时间:2015-08-24 01:57:04

标签: ruby-on-rails ruby json net-http

目标:
将POST请求发送到API端点,并使用Rails修改该资源。我今天已经犯了这个错误差不多6个小时而没有运气;有人可以指出我正确的方向吗?所有相关细节包括:

相关路线的

routes.rb
post '/complete_transportation_request' => 'api/transportation/complete#complete_request'

api / transport / complete_controller.rb (处理此请求的方法:

def complete_request
 @ride = Transportation.find(params["transportation"]["id"])
 @ride.update(transportation_params)
end

我调用POST请求的文件:

require 'net/http'
require 'uri'
....  
def test_post_request(result)
  params = {"transportation"=>{"id"=>6, "state"=>"Complete"}}
  url = URI.parse("http://localhost:3000/complete_transportation_request")
  resp, data = Net::HTTP.post_form(url, params)

  puts resp.inspect
  puts data.inspect
  puts resp.body
  puts params
end
抛出

错误:

ActiveRecord :: RecordNotFound at / complete_transportation_request

  

无法找到带有' id' = id

的运输

app / controllers / api / transportation / complete_controller.rb,第10行

``` ruby
5       @rides = ::Transportation.where(state: "Complete")
6     end
7   
8   
9     def complete_request
>  10       @ride = Transportation.find(params["transportation"]["id"])
11       @ride.update(transportation_params)
12     end
13   
14     private
15   
```

在失败时编辑1:params

[2] pry(#<Api::Transportation::CompleteController>)> params
=> nil

编辑2:失败时的完整pry输出:

Started POST "/complete_transportation_request" for ::1 at 2015-08-23 19:58:49 -0700
Processing by Api::Transportation::CompleteController#complete_request as */*
Parameters: {"transportation"=>"{\"id\"=>\"6\", \"state\"=>\"Complete\"}"}
"Id = id"

From: .... line 11 Api::Transportation::CompleteController#complete_request:

 9: def complete_request
10:     p "Id = #{params["transportation"]["id"]}"
11:     binding.pry
12:     params = JSON.parse(params)
13:     @ride = Transportation.find(params["transportation"]["id"])
14:   @ride.update(transportation_params)
15: end

[1] pry(#<Api::Transportation::CompleteController>)> params
=> nil

编辑3:我错过了什么?参数似乎就在请求日志中!

Started POST "/complete_transportation_request" for ::1 at 2015-08-23 21:37:18 -0700
Processing by Api::Transportation::CompleteController#complete_request as */*
Parameters: {"id"=>"7", "state"=>"Complete"}

From: ...../app/controllers/api/transportation/complete_controller.rb @ line 11 Api::Transportation::CompleteController#complete_request:

10: def complete_request
=> 11:  binding.pry
12:     params = JSON.parse(params)
13:     @ride = Transportation.find(params["transportation"]["id"])
14:   @ride.update(transportation_params)
15: end

[1] pry(#<Api::Transportation::CompleteController>)> params
=> nil

1 个答案:

答案 0 :(得分:0)

&#34>编辑2&#34;中的问题:确保请求中的content-typeaccepts标头包含application/json,而不是text/json,或者你没有做过一些奇怪的事情,比如插入一个&#34;运输&#34;中的json原始字符串。基本HTTP表单中的文本字段。如果您仍然遇到问题,请在最后添加.json,然后才能使用

编辑3&#34;中的问题,请尝试查找params['id']而不是params['transportation']['id']

此外,你将各种东西混合在一起并且不能很好地混合,post_form发送一个表单,就像在HTML页面上一样,Rails中的表单通常是嵌套的 - 它们有一个根节点,如&# 39;运输&#39;,XML也是嵌套的,除非你插入额外的中间件来强制它,否则JSON不会嵌套,

使用curl来测试您的控制器:

curl -H "Content-Type: application/json" -H "Accept: application/json" -k -i -d '{"id": "2874"}' http://localhost:3000/complete_transportation_request.json

并使用find params['id']获取记录