【发布时间】:2019-05-17 16:11:23
【问题描述】:
这是使用Net::HTTP::Post的代码
request = Net::HTTP::Post.new(url)
...
form_data = [
['attachments[]', File.open('file1.txt')],
['attachments[]', File.open('file2.txt')]
]
request.set_form form_data, 'multipart/form-data'
http.request(request)
现在,我正在尝试使用httparty,如下所示,但它不起作用。
body = { attachments: [ File.open('file1.txt'), File.open('file2.txt') ] }
HTTParty.post(url, body: body)
我从网络服务调用得到的响应如下:
#<HTTParty::Response:0x557d7b549f90 parsed_response={"error"=>true, "error_code"=>"invalid_attachment", "error_message"=>"Attachmen
t(s) not found or invalid."}, @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"server"=>["nginx"], "date"=>[
"Mon, 20 May 2019 07:41:50 GMT"], "content-type"=>["application/json"], "content-length"=>["102"], "connection"=>["close"], "vary"=>["
Authorization"], "set-cookie"=>["c18664e1c22ce71c0c91742fbeaaa863=uv425hihrbdatsql1udrlbs9as; path=/"], "expires"=>["Thu, 19 Nov 1981
08:52:00 GMT", "-1"], "cache-control"=>["no-store, no-cache, must-revalidate", "private, must-revalidate"], "pragma"=>["no-cache", "no
-cache"], "x-ratelimit-limit"=>["60"], "x-ratelimit-remaining"=>["59"], "strict-transport-security"=>["max-age=63072000; includeSubdom
ains;"]}>
它似乎无法读取文件的内容。 HTTParty 是否支持这个或者我需要使用其他一些 gem?
【问题讨论】:
-
你能发布完整的回溯错误吗?你确定你点击的网址有效吗?
-
你试过了吗,form_data = [ ['attachments[0]', File.open('file1.txt')], ['attachments[1]', File.open('file2. txt')] ]
-
文件是否有错误的数据或格式?
标签: ruby-on-rails ruby web-services httpclient httparty