RestClient从IO分段上传
我正在尝试使用 RestClient 将数据作为多部分上传,如下所示:
response = RestClient.post(url, io, {
:cookies => {
'JSESSIONID' => @sessionid
},
:multipart => true,
:content_type => 'multipart/form-data'
})
< code>io 参数是一个包含我的文件的 StringIO,因此它来自内存而不是来自磁盘。
服务器(Tomcat servlet)无法读取多部分数据,并给出错误:
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
所以我相信 RestClient 没有以多部分格式发送它?有人看到问题了吗?我假设问题出在 Ruby(客户端)端,但如果有人认为这可能是服务器端问题,我可以发布我的 servlet(Spring)代码。
我还想知道 RestClient 将使用什么作为上传的文件名,因为没有实际的文件...您可以有一个没有文件名的多部分请求吗?
I am trying to upload data as multipart using RestClient like so:
response = RestClient.post(url, io, {
:cookies => {
'JSESSIONID' => @sessionid
},
:multipart => true,
:content_type => 'multipart/form-data'
})
The io
argument is a StringIO that contains my file, so it's from memory instead of from the disk.
The server (Tomcat servlet) is unable to read the multipart data, giving an error:
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
So I believe that RestClient is not sending it in multipart format? Anyone see the problem? I am assuming the problem is on the Ruby (client) side, but I can post my servlet (Spring) code if anyone thinks it might be a server-side problem.
I also wonder what RestClient would use for the uploaded filename, since there isn't an actual file... Can you have a multipart request without a filename?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做到这一点,它只需要子类化
StringIO
并为其添加一个非零路径方法:我刚刚检查过这一点,并且 Mailgun api 对此非常满意。
You can do this, it simply requires subclassing
StringIO
and adding a non-nil path method to it:I've just checked this, and the Mailgun api is pretty down with this.
在咨询了rest-client库(Archiloque)的作者后,似乎如果这是可能的,那么API并没有设置来轻松处理它。使用 :multipart => true 参数将导致 IO 被视为文件,并且它会在 IO 上查找非 nil #path,对于 StringIO 来说,该 #path 始终为 nil。
如果将来有人需要此功能,您需要咨询图书馆的邮件列表 ([电子邮件受保护]),作为作者似乎认为这是可能的,但也许并不简单。
只要它不是多部分格式,它就可以轻松地从 IO 进行流式上传,这就是我最终解决的问题。
After consulting with the author of the rest-client library (Archiloque), it seems that if this is possible, the API is not set up to handle it easily. Using the :multipart => true parameter will cause the IO to be treated like a file, and it looks for a non-nil #path on the IO, which for a StringIO is always nil.
If anyone needs this in the future, you'll need to consult with the library's mailing list ([email protected]), as the author seems to think it is possible but perhaps not straightforward.
It CAN easily do streaming uploads from an IO as long as it's not multipart format, which is what I ended up settling for.