如何在响应到达URL时自动保存JSON响应数据?

时间:2015-05-04 06:39:16

标签: ruby-on-rails json api ruby-on-rails-4 response

我向第三方网址发送请求。收到我的请求后,他们会向我的网址发送回复。我想在响应到达我的URL时自动将该数据保存到我的表中。

回复网址格式:

http://example.com/delivery_reports/new?data=%7B%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A%7B%22911234567890%22%3A%7B%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22%7D%7D%7D 

我的控制器:

def new        
    json = JSON.parse(params["data"])    
    requestId = json["requestId"]
    numbers = json['numbers']

    numbers.each do |num| 
      @delivery_report = DeliveryReport.new
      @delivery_report.request_id = requestId
      @delivery_report.date = num[1]["date"]
      @delivery_report.receiver = num[0].to_s
      @delivery_report.status = num[1]["status"]
      @delivery_report.description = num[1]["desc"]
      @delivery_report.save
    end     
  end

当我直接在网址中输入回复代码时,数据已保存到我的表格中,但在点击网址时不会自动保存。

日志:

 Started GET "/delivery_reports/new" for 49.207.185.206 at 2015-05-04 09:55:34 +0000
2015-05-04T09:55:34.368716+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
2015-05-04T09:55:34.369910+00:00 app[web.1]:   app/controllers/delivery_reports_controller.rb:17:in `new'
2015-05-04T09:55:34.369913+00:00 app[web.1]: 
2015-05-04T09:55:34.369905+00:00 app[web.1]: 
2015-05-04T09:55:34.367718+00:00 app[web.1]: Processing by DeliveryReportsController#new as HTML
2015-05-04T09:55:34.369912+00:00 app[web.1]: 
2015-05-04T09:55:34.369908+00:00 app[web.1]: TypeError (no implicit conversion of nil into String):
2015-05-04T09:55:34.657079+00:00 heroku[router]: at=info method=GET path="/delivery_reports/new" host=blooming-plains-7422.herokuapp.com request_id=d386f035-9abd-4bce-ae98-696e0038f0cd fwd="49.207.185.206" dyno=web.1 connect=1ms service=19ms status=500 bytes=1754
2015-05-04T09:55:34.655482+00:00 app[web.1]: TypeError (no implicit conversion of nil into String):
2015-05-04T09:55:34.648450+00:00 app[web.1]: Processing by DeliveryReportsController#new as HTML
2015-05-04T09:55:34.655478+00:00 app[web.1]: 
2015-05-04T09:55:34.655486+00:00 app[web.1]: 
2015-05-04T09:55:34.655483+00:00 app[web.1]:   app/controllers/delivery_reports_controller.rb:17:in `new'
2015-05-04T09:55:34.655485+00:00 app[web.1]: 
2015-05-04T09:55:34.645606+00:00 app[web.1]: Started GET "/delivery_reports/new" for 49.207.185.206 at 2015-05-04 09:55:34 +0000
2015-05-04T09:55:34.649265+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
2015-05-04T09:55:36.207273+00:00 heroku[router]: at=info method=GET path="/delivery_reports" host=blooming-plains-7422.herokuapp.com request_id=4f96d14d-b4f4-47dd-af63-44acba98e408 fwd="49.207.185.206" dyno=web.1 connect=1ms service=30ms status=200 bytes=2839
2015-05-04T09:55:36.198316+00:00 app[web.1]:   DeliveryReport Load (10.1ms)  SELECT `delivery_reports`.* FROM `delivery_reports`
2015-05-04T09:55:36.181077+00:00 app[web.1]: Started GET "/delivery_reports" for 49.207.185.206 at 2015-05-04 09:55:36 +0000
2015-05-04T09:55:36.201309+00:00 app[web.1]: Completed 200 OK in 14ms (Views: 3.5ms | ActiveRecord: 10.1ms)
2015-05-04T09:55:36.186741+00:00 app[web.1]: Processing by DeliveryReportsController#index as HTML
2015-05-04T09:55:36.200260+00:00 app[web.1]:   Rendered delivery_reports/index.html.erb within layouts/application (12.3ms)
2015-05-04T09:57:49.734086+00:00 heroku[router]: at=info method=GET path="/delivery_reports" host=blooming-plains-7422.herokuapp.com request_id=967fcaf4-36aa-4c39-ae40-07859d3b9a89 fwd="49.207.185.206" dyno=web.1 connect=2ms service=28ms status=200 bytes=2839
2015-05-04T09:57:49.708444+00:00 app[web.1]: Started GET "/delivery_reports" for 49.207.185.206 at 2015-05-04 09:57:49 +0000
2015-05-04T09:57:49.724911+00:00 app[web.1]: Completed 200 OK in 4ms (Views: 3.0ms | ActiveRecord: 0.5ms)
2015-05-04T09:57:49.722539+00:00 app[web.1]:   DeliveryReport Load (0.5ms)  SELECT `delivery_reports`.* FROM `delivery_reports`
2015-05-04T09:57:49.720397+00:00 app[web.1]: Processing by DeliveryReportsController#index as HTML
2015-05-04T09:57:49.724189+00:00 app[web.1]:   Rendered delivery_reports/index.html.erb within layouts/application (2.5ms)
2015-05-04T09:57:49.991163+00:00 heroku[router]: at=info method=GET path="/assets/application-0723cb9a2dd5a514d954f70e0fe0b89f6f9f1ae3a375c182f43b5f2b57e9c869.css" host=blooming-plains-7422.herokuapp.com request_id=129f86b0-b6e2-4637-9f5c-cc4bc3df856c fwd="49.207.185.206" dyno=web.1 connect=1ms service=8ms status=304 bytes=133
2015-05-04T09:57:50.218280+00:00 heroku[router]: at=info method=GET path="/assets/application-dfda1387e2621704c9c53ddd9c1573c1f5099fd3108a2a96d2b02d9b79203f15.js" host=blooming-plains-7422.herokuapp.com request_id=38108dcf-fe1f-416e-a013-5692c84a1794 fwd="49.207.185.206" dyno=web.1 connect=1ms service=5ms status=304 bytes=133
2015-05-04T09:57:52.456405+00:00 heroku[router]: at=info method=GET path="/delivery_reports" host=blooming-plains-7422.herokuapp.com request_id=c04fa439-d812-4214-91d6-8d4ca34be7d0 fwd="49.207.185.206" dyno=web.1 connect=3ms service=22ms status=200 bytes=2839
2015-05-04T09:57:52.437697+00:00 app[web.1]: Started GET "/delivery_reports" for 49.207.185.206 at 2015-05-04 09:57:52 +0000
2015-05-04T09:57:52.446959+00:00 app[web.1]:   DeliveryReport Load (1.5ms)  SELECT `delivery_reports`.* FROM `delivery_reports`
2015-05-04T09:57:52.449426+00:00 app[web.1]: Completed 200 OK in 5ms (Views: 3.2ms | ActiveRecord: 1.5ms)
2015-05-04T09:57:52.443892+00:00 app[web.1]: Processing by DeliveryReportsController#index as HTML
2015-05-04T09:57:52.448588+00:00 app[web.1]:   Rendered delivery_reports/index.html.erb within layouts/application (3.5ms)
2015-05-04T09:57:52.719122+00:00 heroku[router]: at=info method=GET path="/assets/application-0723cb9a2dd5a514d954f70e0fe0b89f6f9f1ae3a375c182f43b5f2b57e9c869.css" host=blooming-plains-7422.herokuapp.com request_id=8efc9c84-b49a-4564-b2d9-b03fabbce459 fwd="49.207.185.206" dyno=web.1 connect=5ms service=9ms status=304 bytes=133
2015-05-04T09:57:52.721796+00:00 heroku[router]: at=info method=GET path="/assets/application-dfda1387e2621704c9c53ddd9c1573c1f5099fd3108a2a96d2b02d9b79203f15.js" host=blooming-plains-7422.herokuapp.com request_id=6de87b4d-12ff-448a-9d2f-38ba6e20a15d fwd="49.207.185.206" dyno=web.1 connect=1ms service=11ms status=304 bytes=133

2 个答案:

答案 0 :(得分:0)

如果在您通过浏览器访问链接时有效,但在网址被点击时却无效,因为API使用POST请求。

为您的routes.rb添加路线:

post 'delivery_reports/new', to: 'delivery_reports#new'

答案 1 :(得分:0)

查看以下代码段。这应该是格式。 Json格式化应根据rails常规param名称进行格式化。你可以试试。

if params[:data].present?    
  request = params[:data][:request_id]
  numbers = params[:data][:number]
  #....

可能的调试方法:

  1. 或者您可以尝试使用pry gem并将binding.pry放入您的操作块中,然后您可以检查正确的参数。并检查您的记录保存过程。
  2. 您可以使用基于浏览器的gem来模拟具有相同数据的api命中,以在本地计算机上生成此问题,以实现您期望的完全相同的api命中所需的行为。
  3. 你可以添加一个raise params.inspect来调试你的param。但是,这可能无法充分说明您的代码可能会中断的原因。