如何使用Ruby中的httpclient发送多部分流请求



我正在尝试获取一个与httpclient合作的多部分流帖。我遇到两个问题。

问题1 :首先,如果我尝试通过文档中描述的方式执行普通帖子;我通过httpbin.org返回我看到这发生了:

代码

        File.open(path_to_my_file) do |file|
          body = [{ 'Content-Type' => 'application/atom+xml; charset=UTF-8',
                    :content => '<entry>...</entry>' },
                  { 'Content-Type' => 'video/mp4',
                    'Content-Transfer-Encoding' => 'binary',
                    :content => file }]
          res = @http_client.post('http://httpbin.org/post', body: body)
          response = res
          puts res.body

结果

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "{"Content-Type"=>"application/atom+xml; charset=UTF-8", :content=>"<entry>...</entry>"}": "", 
    "{"Content-Type"=>"video/mp4", "Content-Transfer-Encoding"=>"binary", :content=>#<File:{{path_to_file}}>}": ""
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "322", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Date": "Mon, 20 May 2019 06:43:17 GMT", 
    "Host": "httpbin.org", 
    "User-Agent": "HTTPClient/1.0 (2.8.3, ruby 2.6.0 (2018-12-25))"
  }, 
  "json": null, 
  "origin": "IP, IP", 
  "url": "https://httpbin.org/post"
}

,如您所见,我看不到文件的内容,而只有包含URI到文件的标识符;因此,我不知道如何修复它,以便我可以看到并返回内容。据我所知,看起来它试图将文件对象视为字符串,这当然不是我想要的。

问题2:每当我动态创建身体时,也就是;用我的代码中设置的对象动态创建哈希阵列,然后我尝试异步发送(我相信这是使其在ruby中以httpclient进行流式传输的正确方法,尽管我不确定我不确定(它将整个身体作为数据发送,而不再是表格或标题。

代码

        request_body = []
        body.files.each do |k, v|
          request_body.push( { 'Content-Type' => v.content_type, :content => v.content })
        end
        body.values.each { |k, v| request_body << { k.to_sym => v }}
        #This creates the array correctly, although I just wanted to show how it was generated
        connection = @http_client.send(method + '_async', uri, body: request_body, header: headers)
        response = connection.pop
        # Starts reading result here

响应

  "args": {}, 
  "data": "%7B%22Content-Type%22%3D%3E%22video%2Fmp4%22%2C+%3Acontent%3D%3E%23%3CFile%3A%2Fhome%2Fuser%2Ffiles%2file.mp4%3E%7D=&%7B%3Avalue%3D%3E%22hello+world%22%7D=", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "247", 
    "Content-Type": "application/json", 
    "Date": "Mon, 20 May 2019 06:44:11 GMT", 
    "Host": "httpbin.org", 
    "User-Agent": "HTTPClient/1.0 (2.8.3, ruby 2.6.0 (2018-12-25))", 
    "ApplicationIdentifier": "identifier"
  }, 
  "json": null, 
  "origin": "IP, IP", 
  "url": "https://httpbin.org/post"
}

如您所见,它将所有内容都放入数据中,老实说,我不知道该如何将其作为形式而不是作为数据发送。我将文件作为文件对象发送。我尝试将其作为普通帖子而不是post_async发送,但似乎无法正常工作。

有人以前遇到过这些问题,并且知道我应该如何解决这些问题?(或者您可以看到我出错的地方,以便我至少可以尝试进一步进一步(

预先感谢

关于您的第一个问题,看来文档不正确。我已经执行了一些测试,并且内容类型似乎是应用程序/X-WWW-Form-urlenCoded。您需要明确设置内容类型:

body: body, header: {'Content-Type': 'multipart/form-data; boundary=ABC'}

这还不够。您还需要手动设置内容处置。例如:

      body = [{ 'Content-Type' => 'application/atom+xml; charset=UTF-8',
                'Content-Disposition' => 'form-data; name="name1"',
                :content => '<entry>...</entry>' },
              { 'Content-Type' => 'video/mp4',
                'Content-Disposition' => 'form-data; name="file"; filename="myfile.pdf"',
                'Content-Transfer-Encoding' => 'binary',
                :content => file }]

这样,httpbin报告了一个文件和一个表单参数。

您的第二个问题与同一问题有关。您的文件和值都需要设置内容处置。例如(快速而肮脏,样式可能会更好(:

files.each do |k, v|
    request_body << { 'Content-Type' => v.content_type, :content => v.content, 'Content-Disposition' => 'form-data; name="' + k + '"; filename="' + v.filename + '"' }
end
values.each { |k, v| request_body << { :content => v, 'Content-Disposition' => 'form-data; name="' + k + '"' }}

请注意,您应该逃脱任何"名称或文件名中的任何"。

最新更新