HTTP POST multipart with named file

六月ゝ 毕业季﹏ 提交于 2019-12-10 20:38:38

问题


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.

Thanks!


回答1:


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.




回答2:


I managed to get it working with

HTTPoison.post!(url, {:multipart, [{"name", "value"}, {:file, path_to_file}]})

with some help from this Github issue



来源:https://stackoverflow.com/questions/33557133/http-post-multipart-with-named-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!