Ruby:如何使用 Curb 发送 JSON POST 请求?

2024-02-26

如何将 CURB 请求的请求正文设置为我的 json 字符串? 我正在尝试使用 Curb 执行 JSON POST 请求。

My code:

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 设置为请求正文。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ruby:如何使用 Curb 发送 JSON POST 请求? 的相关文章

随机推荐