我正在尝试发布请求使用httparty gem。
response = HTTParty.post(App.base_uri + "/api?service=post&action=update&version=2",
body: [{"some_big_json": "with many key value pair"}],
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Cookie' => @@cookie
})
Body是一个数组。当我发出发布请求时,出现此错误:
/home/user/.rbenv/versions/2.5.0/lib/ruby/2.5.0/net/http/generic_request.rb:183:in send_request_with_body': undefined method bytesize' for #<Array:0x00005561f96833e0> (NoMethodError)
如何使用httparty在体内张贴大型阵列?
更新:在邮递员中,我可以将正文作为数组发送,并且我获得了成功(预期)的响应。
答案 0 :(得分:1)
尝试传递字符串而不是数组。
response = HTTParty.post(App.base_uri + "/api?service=post&action=update&version=2",
body: "[{'some_big_json': 'with many key value pair'}]",
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Cookie' => @@cookie
})
答案 1 :(得分:1)
您也可以尝试RestClient
RestClient::Request.execute(method: :post,
url: App.base_uri + '/api',
payload: params,
timeout: 1000,
headers: headers)
答案 2 :(得分:0)
只需在提交之前将数组转换为JSON。
options = @options.merge(
body: [
{
name: 'John Doe',
}
].to_json
)