HTTP POST多部分与命名文件 [英] HTTP POST multipart with named file

查看:162
本文介绍了HTTP POST多部分与命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送一个(多部分)HTTP请求,其中包含一个名为的文件。这似乎比我想象的更难实现......我试图用HTTPoison找到一种方法,但我无法使用文件以外的名称。我已经尝试直接使用Hackney,但似乎没有是一个选项,并且肯定没有测试任何一个显示此功能。我也看过ibrowse和HTTPotion,但找不到任何看似有用的东西(我的Erlang非常有限,请注意)。使用Ruby库以下是我想要做的一个示例遏制(注意Curl :: PostField.file采用名称和文件路径)。

I need to send a (multipart) HTTP request which contains a file which is named. This seems to be harder to achieve than I imagined... I've tried to figure out a way to do it with HTTPoison, but I can't get it to use a name other than "file". I've tried using Hackney directly, but there doesn't appear to be an option, and there definitely isn't a test on either of these which shows this functionality. I've also had a look at ibrowse and HTTPotion but can't find anything which seems useful (my Erlang is very limited, mind you). Here is an example of what I want to do, with the Ruby library Curb (note the Curl::PostField.file takes a name and a file path).

这是一件奇怪的事吗?或者我错过了一些明显的东西...非常感谢任何建议。

Is this such a strange thing to do? Or am I missing something obvious here... Any suggestion is greatly appreciated.

谢谢!

推荐答案

如果将来有人遇到这个问题,这就是解决方案:

In case anyone in the future encounters this problem, here's the solution:

HTTPoison.start
request = HTTPoison.post!(url, {:multipart, [{:file, "path/to/file", { ["form-data"], [name: "\"photo\"", filename: "\"/path/to/file\""]},[]}]}, headers, options)

请注意额外的转义报价。

Note the extra escaped quotes.

这篇关于HTTP POST多部分与命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆