Ruby on rails 如何在HttpParty中构建json参数
我必须在httparty中发送post请求并获得响应 我的json参数是Ruby on rails 如何在HttpParty中构建json参数,ruby-on-rails,json,httparty,Ruby On Rails,Json,Httparty,我必须在httparty中发送post请求并获得响应 我的json参数是 { "webhook": { "topic": "shop/update", "address": "http://2350d6c0.ngrok.io/shopify_stores/update_shop_info", "format": "json" } } 我使用httparty参数作为 begin response = HTTParty.post("#{u
{
"webhook": {
"topic": "shop/update",
"address": "http://2350d6c0.ngrok.io/shopify_stores/update_shop_info",
"format": "json"
}
}
我使用httparty参数作为
begin
response = HTTParty.post("#{url}",
{
:body => [
{
"webhook" => {
"topic" => "shop\/update",
"address" => "http:\/\/15ec3a12.ngrok.io\/shopify_stores\/update_shop_info", #place your url here
"format" => "json"
}
}
].to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
byebug
rescue HTTParty::Error
end
但它代表的是
必需的参数丢失或无效使用HTTParty最平滑的方法是创建客户端,而不是以过程方式使用它
class MyApiClient
include HTTParty
base_uri 'example.com'
format :json
# this is just an example of how it is commonly done
def initalize(api_key = nil)
@api_key = api_key
end
def post_something(data)
self.class.post("/some_path", data)
end
end
这将使您能够:
client = MyApiClient.new()
puts client.post_something({ foo: "bar" })
您不需要处理设置标题或编码正文-HTTParty将为您处理这些。这就是这个库的全部要点——如果您想按程序将其输出,只需使用stdlib的一部分即可 嗯。控制器中是否有部分在
private
部分下显示def something_params
?若有的话,你们能把它放在你们的问题里吗?我并没有使用它们,只是向服务器发送一个post请求并保存回复