Ruby on rails 如何从外部API接收帖子内容
将发送如下所示的回发:Ruby on rails 如何从外部API接收帖子内容,ruby-on-rails,api,post,Ruby On Rails,Api,Post,将发送如下所示的回发: { "api_key": "keykeykeykeykey", "environment": "production", "postback_url": "http://example.com/postbacks", "blueprint": "research_link_data", "input": { "name": "example name" "website": "example website" } "status
{
"api_key": "keykeykeykeykey",
"environment": "production",
"postback_url": "http://example.com/postbacks",
"blueprint": "research_link_data",
"input": {
"name": "example name"
"website": "example website"
}
"status": "processing",
"output": {
"Correction": "example Correction"
"has_book": "example has_book"
"search_results_link": "example search_results_link"
}
}
(使用真正的API_密钥。)
(当您向API发送初始请求时,您可以自行设置postback\u url
。)
为了接收和处理这些回发,我有:
class Postback < ActiveRecord::Base
attr_accessible :uuid
belongs_to :survey
extend FriendlyId
friendly_id :uuid
after_create :generate_uuid
def generate_uuid
self.update_attributes :uuid => SecureRandom.uuid
end
end
class PostbacksController < ApplicationController
respond_to :html
def receive
@postback = Postback.find(params[:id])
end
end
Testivate::Application.routes.draw do
resources :postbacks, :only => [:index, :show] do
member do
post :receive
end
end
end
我怎样才能得到剩下的结果
谢谢
Steven.这意味着您必须设置页眉字段才能使表单post正常工作:
“将内容类型:application/x-www-form-urlencoded添加到标题”答案是:
respond\u to:html,:JSON
)Content-Type:application/json-Accept:application/json
六羟甲基三聚氰胺六甲醚。。。谢谢你的链接。我试过了,现在就这么做。但是我收到的情人和我已经收到的没什么不同。酷。该页面上狡猾的屏幕截图似乎还暗示,您应该在数据字段中使用url编码格式,例如“key=value&other_key=other_value”,而不是使用的哈希语法。(注意:我只是猜测)更新:如果我将标题设置为“Content-Type:application/json-Accept:application/json”,我会得到一个“MultiJson::LoadError at/postbacks/32e5a1bb-452f-4ad2-9a42-c17239f3d964/receive…意外令牌”错误。现在用谷歌搜索那个。
> params
=> {"action"=>"receive",
"controller"=>"postbacks",
"id"=>"32e5a1bb-452f-4ad2-9a42-c17239f3d964"}