Support curl embed content of a file as text field in multipart/form-data request
Problem to solve
I want to send multipart/form-data request and set the contents for a text field from a file.
Don't attach it as a file upload by adding a filename= to the Content-Disposition.
Instead of
--delimiter
Content-Disposition: form-data; name="field1"; filename="value1.txt"
value1
--delimiter--
i want
--delimiter
Content-Disposition: form-data; name="field1"
value1
--delimiter--
Proposal
Use embedfile instead of file:
POST https://example.org/api
[MultipartFormData]
field1: embedfile,value1.txt
Additional context and resources
Curl`s documentation https://curl.se/docs/manpage.html#-F
To just get the content part from a file, prefix the filename with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.
Hi @meixger
Do I understand correctly your use case if I emulate it by injecting a variable:
$ value1=`cat file.txt`
$ hurl --variable value1="$value1" form.hurl
with form.hurl being:
POST http://localhost:8000
[MultipartFormData]
field1: {{value1}}
Instead of injecting the value of file.txt, you want to be able to do:
POST http://localhost:8000
[MultipartFormData]
field1: {{ getFile "file.txt" }}
Hi @jcamiel
Do I understand correctly your use case if I emulate it by injecting a variable:
Yes.
I assume the proposed function getFile would load the content of file.txt and insert it as the value of field1.
Yes exactly, we have "functions" today https://hurl.dev/docs/templates.html#functions, we can imagine having a function getFile that returns the content of a file.
Some things to not forget (for the implementation)
- content of the file: binary vs text
- security: restrict the ablity to upload any local file i.e.: forbid
{{ getFile "/etc/passwd" }}