简体   繁体   English

Rest-Client:如何发布多部分/表单数据?

[英]Rest-Client: how to post multipart/form-data?

I have to implement the curl POST request below listed, in Ruby, using Rest-Client. 我必须使用Rest-Client在Ruby中实现下面列出的curl POST请求。

I have to: 我必须:

  • send params in header; 在标头中发送参数;
  • send params (that do not contain a file) as multipart/form-data : 发送参数(不包含文件)作为multipart/form-data

     $ curl -X POST -i -H "Authorization: Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001" \\ https://api.somewhere.com/endpoint -F sku_id=608399 

How can I translate the curl request using the RestClient rubygem? 如何使用RestClient ruby​​gem转换curl请求?

Reading documentation (multipart paragraph): https://github.com/rest-client/rest-client I coded as: 阅读文档(多部分): 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}

But I get a server error; 但是我收到服务器错误; is the Ruby code above correct? 上面的Ruby代码正确吗?

Thanks a lot, Giorgio 非常感谢,乔治

Since I had trouble understanding the example Dmitry showed, here is an example for creating a Multipart request to upload an image: 由于我无法理解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}}

It's code not valid for RestClient implementation. 该代码对RestClient实现无效。 headers should follow after payload . headers应在payload之后。

module RestClient 
 def self.post(url, payload, headers={}, &block)
  ...
 end
end

UPDATE 更新

@access_token should be a string "2687787877876666686b213e92aa3ec7e1afeeb560000000001" @access_token应该是字符串"2687787877876666686b213e92aa3ec7e1afeeb560000000001"

then 然后

RestClient.log = 'stdout'
RestClient.post url, {:sku_id => 608399, :multipart => true}, req

and log 并记录

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM