【问题标题】:Faraday multipart request with json & file data带有 json 和文件数据的法拉第多部分请求
【发布时间】:2018-02-08 07:12:04
【问题描述】:

当尝试使用 Faraday gem POST 多部分数据(文件和 json)时,远程服务器无法识别 json 数据参数并且由于它是必需参数而失败并出现验证错误。

connection = Faraday.new(url: "#{uri.scheme}://#{uri.host}:#{uri.port}") do |faraday|
              faraday.request :multipart
              faraday.request :url_encoded

              faraday.adapter :net_http
            end

request_data = { file: Faraday::UploadIO.new('somefile.png', 'image/png'), jsonBody: JSON.dump({ id: 'foo', name: 'bar' }) }

response = connection.post(uri.request_uri) do |request|
  request = Ff::Api::RequestHeaders.set(request, self.api_options)
  request.headers['content-type'] = 'multipart/form-data; boundary=-----------RubyMultipartPost'
  request.body = request_data
end

工作 CURL 请求:

curl -v -H 'Authorization: bearer 7d70fb31-0eb9-4846-9ea8-933dfb69d8f1' \
-H 'Accept: application/xml,application/xhtml+xml,application/json,text/html,text/plain' \
-H 'Content-Type: multipart/form-data' \
-F 'jsonBody={"id":"foo","name":"bar"};type=application/json' \
'http://localhost:8080/localproject/rest-resource/items'

卷曲日志:

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /localproject/rest-resource/items HTTP/1.1
> Host: localhost:8080
> Authorization: bearer 7d70fb31-0eb9-4846-9ea8-933dfb69d8f1
> USER_IP_ADDR: 127.0.0.1
> Accept: application/xml,application/xhtml+xml,application/json,text/html,text/plain
> Content-Length: 555
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------0c5f3acb5a4731ff
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: DENY
< X-Content-Type-Options: nosniff
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Date: Thu, 08 Feb 2018 10:50:06 GMT
< 
* Connection #0 to host localhost left intact
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><updateResponse><id>5ce0b335-4638-474c-8c4a-9adb6c54b057</id><success>true</success></updateResponse>% 

请求正文的附加前缀“type=application/json”似乎与 CURL 有所不同。没有找到任何方法对法拉第做同样的事情。

法拉第版本:0.12.2 & 0.14.0

Ruby 版本:2.3.3

导轨:5.2

任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过来自终端的 CURL 请求有效?尝试将法拉第请求的日志与 CURL 进行比较。如果您在问题中发布日志可能会有所帮助。
  • @aBadAssCowboy 更新了问题以包含 curl 示例和日志。

标签: ruby-on-rails ruby faraday


【解决方案1】:

发现 Faraday 不支持混合多部分请求(文件上传带有 json/xml 数据)。这是因为混合文件和 json 数据不是一种安静的方法,应该完全避免。更多讨论在这里进行https://github.com/lostisland/faraday/issues/769

我已通过修补 Faraday::Mulitipart 类以允许混合 JSON 数据暂时解决此问题,

class Faraday::Request::Multipart
  def create_multipart(env, params)
    boundary = env.request.boundary
    parts = process_params(params) do |key, value|
      if (JSON.parse(value) rescue false)
        Faraday::Parts::Part.new(boundary, key, value, 'Content-Type' => 'application/json')
      else
        Faraday::Parts::Part.new(boundary, key, value)
      end
    end
    parts << Faraday::Parts::EpiloguePart.new(boundary)

    body = Faraday::CompositeReadIO.new(parts)
    env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
    return body
  end
end

但从长远来看,应该更改服务器实现以避免混合多部分请求。

【讨论】:

    【解决方案2】:

    不确定这是否是其他人的选择,但我尝试发布到的服务器将接受 json 作为文件部分(而不是消息正文)。在这种情况下,这样的事情会起作用:

    require 'faraday'
    
    conn = Faraday.new("http://server:port") do |f|
        f.request:multipart
        f.request:url_encoded
        f.adapter:net_http
    end
    
    payload = {
        filePart:Faraday::UploadIO.new("image.jpg", 'image/jpeg'),
        jsonPart:Faraday::UploadIO.new("stamp.json", 'application/json')
    }
    
    res = conn.post('/my/url/', payload)
    puts res.body
    

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 1970-01-01
      • 2023-01-23
      • 2014-02-15
      • 2020-04-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多