3

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!

Peer Stritzinger
  • 8,232
  • 2
  • 30
  • 43
Zen
  • 7,197
  • 8
  • 35
  • 57

2 Answers2

13

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.

Zen
  • 7,197
  • 8
  • 35
  • 57
  • Pay attention to the double quotation marks escapes for name and filename, they are kind of mandatory, instead HTTPoison wont properly forge the request. – Cristian Montini Sep 12 '22 at 10:54
1

I managed to get it working with

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

with some help from this Github issue

Daniel Corin
  • 1,987
  • 2
  • 15
  • 27