如何将 CURB 请求的请求主体设置为我的 json 字符串? 我正在尝试使用 Curb 发出 JSON POST 请求。
我的代码:
require 'rubygems'
require 'curb'
require 'json'
myarray = {}
myarray['key'] = 'value'
json_string = myarray.to_json()
c = Curl::Easy.http_post("https://example.com"
# how do I set json_string to be the request body?
) do |curl|
curl.headers['Accept'] = 'application/json'
curl.headers['Content-Type'] = 'application/json'
curl.headers['Api-Version'] = '2.2'
end
最佳答案
正确的做法是在URL后面加上内容:
c = Curl::Easy.http_post("https://example.com", json_string_goes_here
) do |curl|
curl.headers['Accept'] = 'application/json'
curl.headers['Content-Type'] = 'application/json'
curl.headers['Api-Version'] = '2.2'
end
这会将 json_string 设置为请求正文。
关于Ruby:如何使用 Curb 发送 JSON POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851445/