Ruby:縁石を使用してJSON POSTリクエストを送信するにはどうすればよいですか?
質問
縁石リクエストのリクエスト本文を私のJSON文字列にするにはどうすればよいですか?縁石を使用して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がリクエスト本体になります。
所属していません StackOverflow