![](/img/trans.png)
[英]How to convert a cURL multipart/form-data POST request to ruby request using rest-client?
[英]Rest-Client: how to post multipart/form-data?
我必须使用Rest-Client在Ruby中实现下面列出的curl POST请求。
我必须:
发送参数(不包含文件)作为multipart/form-data
:
$ curl -X POST -i -H "Authorization: Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001" \\ https://api.somewhere.com/endpoint -F sku_id=608399
如何使用RestClient rubygem转换curl请求?
阅读文档(多部分): https : //github.com/rest-client/rest-client我编码为:
@access_token = 2687787877876666686b213e92aa3ec7e1afeeb560000000001
url = 'https://api.somewhere.com/endpoint'
req = { authorization: "Bearer #{@access_token}"}
RestClient.post url, req, {:sku_id => 608399, :multipart => true}
但是我收到服务器错误; 上面的Ruby代码正确吗?
非常感谢,乔治
由于我无法理解Dmitry显示的示例,因此以下是创建用于上传图像的Multipart请求的示例:
response = RestClient.post 'https://yourhost.com/endpoint',
{:u_id => 123, :file => File.new('User/you/D/cat.png', 'rb'), :multipart => true},
{:auth_token => xyz5twblah, :cookies => {'_cookie_session_name' => cookie}}
该代码对RestClient
实现无效。 headers
应在payload
之后。
module RestClient
def self.post(url, payload, headers={}, &block)
...
end
end
更新
@access_token
应该是字符串"2687787877876666686b213e92aa3ec7e1afeeb560000000001"
然后
RestClient.log = 'stdout'
RestClient.post url, {:sku_id => 608399, :multipart => true}, req
并记录
RestClient.post "https://api.somewhere.com/endpoint", "--330686\r\nContent-Disposition: form-data; name=\"sku_id\"\r\n\r\n608399\r\n--330686--\r\n", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001", "Content-Length"=>"79", "Content-Type"=>"multipart/form-data; boundary=330686"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.