Faraday: 需要帮助上传 json 和文件作为 multipart/form-data

创建于 2020-01-05  ·  3评论  ·  资料来源: lostisland/faraday

下面是我需要上传的

-data-binary $'

------WebKitFormBoundaryInmulX6ait6ZMLdu\r\n
Content-Disposition: form-data; name="message"; filename="blob"\r\n
Content-Type: application/json
{
 "title":"title of the message to be sent",
 "body":"body of the message to be sent",
 "attachedFileName":null,
 "attachedFileName2":null,
 "attachedFileName3":null,
 "recipientIdList":[186554]
}
 ------WebKitFormBoundaryInmulX6ait6ZMLdu--\r\n' --compressed

请注意服务器接收的内容类型是multipart/form-data

我当前的代码如下所示:

client = Faraday::Connection.new(url: BASE_URL) do |builder|
    builder.use :cookie_jar
    builder.use :multipart
    builder.use :url_encoded
    builder.adapter :net_http
end

message = {title: "title", body: "body of message", recipientIdList:[186554]}
payload = {message: JSON.dump(message)}

response = @client.post(URL) do |request|
    request.headers['Content-Type'] = 'multipart/form-data'
    request.body = payload
end

但是,我从服务器得到的响应是{"success"=>false, "errorCode"=>nil, "message"=>"Content type 'application/octet-stream' not supported", "data"=>nil}

我一点一点地调整代码并再次尝试,并在谷歌搜索了几个小时但找不到解决方案。
如果有人可以帮助我,将不胜感激。
提前谢谢你:D

PS这个问题似乎与https://github.com/lostisland/faraday/issues/830#issue -372589645和https://github.com/lostisland/faraday/issues/769#issue -295426091有关,但我找不到解决方案..请帮助..

最有用的评论

问题在于有效载荷的构造方式。 根据您的curl -data-binary片段,您的服务器希望message表单值具有application/json内容类型。 但是,构造的有效负载没有在任何地方指定这一点。

这是在 Faraday v1.0 中修复它的方法。 了解有关多部分中间件的更多信息……

-payload = {message: JSON.dump(message)}
+payload = { message: Faraday::ParamPart.new(JSON.dump(message), ‘application/json’) }

在 Faraday v0.1x 中(在 0.17.3 中确认)。 请注意,这仍然会发送一个“文件”,它只是从内存中的 IO 对象中读取。 希望服务器不介意额外的多部分标头值。 您可以在此消息底部的第三个请求中查看详细信息。

-payload = {message: JSON.dump(message)}
+{ message: Faraday::UploadIO.new(StringIO.new(JSON.dump(message)), 'application/json') }

我不知道如何处理您的服务器回复的错误消息。

不支持内容类型“应用程序/八位字节流”

我希望修复多部分内容类型问题能够解决它,因为我看不到application/octet-stream的来源。

如果您还有其他问题,请告诉我!

展示我的作品

您可以在multipart.rb · GitHub中查看工作代码片段。 由于代码引用的 RequestBin仅临时保存请求信息,因此我将详细信息记录如下:

负载中没有内容类型的请求:

# AMZ/Cloudfront headers removed
Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d
Connection: close
Connect-Time: 1
X-Request-Id: e802a260-56a9-4d01-a4ee-77f652380185
Content-Length: 253
Host: requestbin.io
User-Agent: Faraday v1.0.0
Accept: */*

——————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d
Content-Disposition: form-data; name=“message”

{“title”:”title”,”body":"body of message”,”recipientIdList":[186554]}
——————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d——————

请求 WITH 内容类型 (Faraday 1.0)

Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4

Connection: close
Connect-Time: 0
Content-Length: 285
User-Agent: Faraday v1.0.0
X-Request-Id: 2bbb3705-42ec-4de2-877c-2afd588ed679
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Host: requestbin.io

——————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4
Content-Disposition: form-data; name=“message”
Content-Type: application/json

{“title”:”title”,”body":"body of message”,”recipientIdList”:[186554]}
——————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4——————

请求 WITH 内容类型 (Faraday v0.17.3)

Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697
Connection: close
Content-Length: 363
User-Agent: Faraday v0.17.3
X-Request-Id: af65fc06-3914-4ab6-8dbf-b347a1ac498a
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Host: requestbin.io

——————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697
Content-Disposition: form-data; name=“message”; filename=“local.path"
Content-Length: 69
Content-Type: application/json
Content-Transfer-Encoding: binary

{“title”:”title”,”body”:”body of message”,”recipientIdList”:[186554]}
——————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697—

所有3条评论

问题在于有效载荷的构造方式。 根据您的curl -data-binary片段,您的服务器希望message表单值具有application/json内容类型。 但是,构造的有效负载没有在任何地方指定这一点。

这是在 Faraday v1.0 中修复它的方法。 了解有关多部分中间件的更多信息……

-payload = {message: JSON.dump(message)}
+payload = { message: Faraday::ParamPart.new(JSON.dump(message), ‘application/json’) }

在 Faraday v0.1x 中(在 0.17.3 中确认)。 请注意,这仍然会发送一个“文件”,它只是从内存中的 IO 对象中读取。 希望服务器不介意额外的多部分标头值。 您可以在此消息底部的第三个请求中查看详细信息。

-payload = {message: JSON.dump(message)}
+{ message: Faraday::UploadIO.new(StringIO.new(JSON.dump(message)), 'application/json') }

我不知道如何处理您的服务器回复的错误消息。

不支持内容类型“应用程序/八位字节流”

我希望修复多部分内容类型问题能够解决它,因为我看不到application/octet-stream的来源。

如果您还有其他问题,请告诉我!

展示我的作品

您可以在multipart.rb · GitHub中查看工作代码片段。 由于代码引用的 RequestBin仅临时保存请求信息,因此我将详细信息记录如下:

负载中没有内容类型的请求:

# AMZ/Cloudfront headers removed
Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d
Connection: close
Connect-Time: 1
X-Request-Id: e802a260-56a9-4d01-a4ee-77f652380185
Content-Length: 253
Host: requestbin.io
User-Agent: Faraday v1.0.0
Accept: */*

——————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d
Content-Disposition: form-data; name=“message”

{“title”:”title”,”body":"body of message”,”recipientIdList":[186554]}
——————RubyMultipartPost-10fbc5eb433c89c0eca87785f09ae45d——————

请求 WITH 内容类型 (Faraday 1.0)

Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4

Connection: close
Connect-Time: 0
Content-Length: 285
User-Agent: Faraday v1.0.0
X-Request-Id: 2bbb3705-42ec-4de2-877c-2afd588ed679
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Host: requestbin.io

——————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4
Content-Disposition: form-data; name=“message”
Content-Type: application/json

{“title”:”title”,”body":"body of message”,”recipientIdList”:[186554]}
——————RubyMultipartPost-9d77ee5700e0658dddb09ac41fad5fa4——————

请求 WITH 内容类型 (Faraday v0.17.3)

Content-Type: multipart/form-data; boundary=—————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697
Connection: close
Content-Length: 363
User-Agent: Faraday v0.17.3
X-Request-Id: af65fc06-3914-4ab6-8dbf-b347a1ac498a
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Host: requestbin.io

——————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697
Content-Disposition: form-data; name=“message”; filename=“local.path"
Content-Length: 69
Content-Type: application/json
Content-Transfer-Encoding: binary

{“title”:”title”,”body”:”body of message”,”recipientIdList”:[186554]}
——————RubyMultipartPost-697c8614200bc0d7ae2fcc0215a2a697—

非常感谢您的帮助, @technoweenie ! 你真棒!!
该请求现在可以与新构建的有效负载一起正常工作,并且不再抱怨application/octet-stream
希望你有一个美好的一天:D

很高兴听到它现在按预期工作!
干得好@technoweenie ,感谢您这么快解决这个棘手的问题👏!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

mattmill30 picture mattmill30  ·  4评论

jedeleh picture jedeleh  ·  3评论

subvertallchris picture subvertallchris  ·  5评论

iMacTia picture iMacTia  ·  3评论

jeffb-stell picture jeffb-stell  ·  5评论