Send file with httpc:request and multipart/form-data (It can be picture and txt also)

Good afternoon, to everyone who reads my letter. I wish you a good day!

I am a beginner Erlang developer, and faced with a problem when sending a file, a picture using httpc:request, I have read a lot of things on the Internet, but did not find a concrete answer on the Internet.

My problem is that I do not quite understand what parameters I need to pass in the body of the request for the server to process my file. If you are reading this and have encountered this problem, but you know some article, please attach it in the comments.

The second problem is that I send my file to the server, and when my file request arrives, its body is sent to the validator, which checks for the “inputfile” key with a value of type file, and also “inputfile_filename” with a value of type string. And I do not understand how to put in one request and sending a picture, and maps with inputfile and inputfile_filename to correctly process my request to the server.

I would be grateful for any help, advice or a link to an article about multipart/form-data.

Once again I wish you a good mood and a good day.

1 Like

What kind of HTTP server software are you using?

In Zotonic we have our own parser routines to stream files to a temporary file on disk. We leave then an #upload{} record in the query arguments to be processed by the controller (or other) functions.

In Cowboy there are also parsing routines.

Here is some documentation that @essen wrote:

Thank you for your response.
I am using cowboy, I have an api to handle my post request.
I want to send a file from Common Test module using httpc:request. Saw this answer how to http:post file with httpc:request in erlang? - Stack Overflow, it would probably work, but my server validator expects also in the body of the request keys like #{inputfile => file type value, and inputfile_filename=> string type values}. And I don’t quite understand how I can pass this all in one request body.
I need to pass in the sheet parameters for multipart/form-data as mentioned in the stackoverflow answer ( generated using the format_multipart_formdata function), but I’m not quite sure where I should put also my keys with the value for the server to properly accept and process.

When my server accepts my file and everything is ok it will return the 200 key and the id for the file in the body of the response. But right now it is returning a response to me in the form of :
Error in validator: #{<<“inputfile”>> => <<“REQUIRED”>>,<<“inputfile_filename”>> => <<“REQUIRED”>>}