Rest-Client:如何发布multipart/form-data?

Gio*_*ino 0 ruby curl multipartform-data rest-client http-headers

我必须使用Rest-Client在Ruby中实现下面列出的curl POST请求.

我必须:

  • 在标题中发送params;
  • 发送params(不包含文件)为multipart/form-data:

    $ curl -X POST -i -H "Authorization: Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001" \
        https://api.somewhere.com/endpoint -F sku_id=608399
    
    Run Code Online (Sandbox Code Playgroud)

如何使用RestClient ruby​​gem翻译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}
Run Code Online (Sandbox Code Playgroud)

但我收到服务器错误; 上面的Ruby代码是否正确?

非常感谢,Giorgio

Lin*_*nju 9

由于我无法理解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}}
Run Code Online (Sandbox Code Playgroud)