我必须在httparty中发送帖子请求并获得回复 我的json params是
{
"webhook": {
"topic": "shop/update",
"address": "http://2350d6c0.ngrok.io/shopify_stores/update_shop_info",
"format": "json"
}
}
我正在使用httparty params
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
但它的回复是
必需参数缺失或无效
答案 0 :(得分:1)
使用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将为您处理。这就是图书馆的全部意义 - 如果你想在程序上扼杀它,只需使用Net::HTTP作为stdlib的一部分。