HTTPoison POST multipart with more form than the file

So I tested, and tested, and didn’t manage to do what I set out to do. Here are my results:

With Nodejs, I get a successful post, as I said before. Here is what it looks like:

POST / HTTP/1.1
host: servIP:8080
content-type: multipart/form-data; boundary=--------------------------374763712004028780220119
content-length: 54609
Connection: close

----------------------------374763712004028780220119
Content-Disposition: form-data; name="chat_id"

237799109
----------------------------374763712004028780220119
Content-Disposition: form-data; name="photo"; filename="photo.jpg"
Content-Type: image/jpg

<photo binary>
----------------------------374763712004028780220119--

Here’s the JS code for the curious

As you can see, it is not a photo object containing a photo, but an item between boundaries named after the object that it’s supposed to be, so photo: binary becomes name=photo <content-type> <binary> and other elements of the form are just presented as different items with a boundary in-between. The boundary is pre-defined at the beginning and two hyphens are added at the start of each boundary and at the end of the multipart post.

Here is a list of all the results (I will summarize them later, so you don’t have to read the contents of the link):

The successful posts (successfully uploaded a file to the server) through HTTPoison never reach the same result. The two closest ones are:

Result A:

POST / HTTP/1.1
Host: servIP:8080
User-Agent: hackney/1.7.1
Content-Type: multipart/form-data; boundary=---------------------------kyorwiszyqolhtih
Content-Length: 883

-----------------------------kyorwiszyqolhtih
content-length: 631
content-type: image/jpeg
content-disposition: form-data; name="file"; filename="pixel.jpg"
name: "photo"
filename: "pixel.jpg"

<photo binary>
-----------------------------kyorwiszyqolhtih--

and

Result B:

POST / HTTP/1.1
Host: servIP:8080
User-Agent: hackney/1.7.1
Content-Type: multipart/form-data; boundary=---------------------------mgvbohsjfwhmfxty
Content-Length: 221

-----------------------------mgvbohsjfwhmfxty
content-length: 15
content-type: application/octet-stream
content-disposition: form-data; name="photo"

files/pixel.jpg
-----------------------------mgvbohsjfwhmfxty--

As you can see on Result A, the metadata that hackney is supposed to add is added not as proper multi-form content-disposition entries but as strange entries with colon notation as name: "photo" instead of name="photo", added after content-disposition. The second one, I didn’t ever manage to upload the file because I could only say {"key", "value"} because {"key": {:file, data}} was rejected by Hackney.

I have no idea how to solve this. Is HTTPoison unable to do this or is it just me being a hopeless noob?

1 Like