hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Support curl ;type magic string in multipart/form-data request

Open fenuks opened this issue 5 months ago • 0 comments

Problem to solve

I want to send multipart/form-data request and set Content-Type for each part. curl allows that adding ;type=<content type> for each --form argument. Example from curl manual: curl -F "name=daniel;type=text/foo" example.com

Proposal

# demo.hurl
POST http://localhost:8000
Content-Type: multipart/form-data
[MultipartFormData]
file: file,demo.hurl;
data: {"some": "json"};type=application/json
HTTP 200

Additional context and resources

Hurl already allows adding content type for files after ;, but only for files, and without type= keyword.

Support can be emulated saving JSON payload to separate file, but it's a bit tedious to have separate file for each JSON part.

Possibly one could also use multipart forms with a multiline string body, but that would work only for text payloads, and in my case, I want to send a binary file, and some JSON with appropriate content type.

If you save example above to demo.hurl, run netcat to listen on port nc -l -p 8000, and then hurl with hurl --verbose demo.hurl, you will see that hurl sends JSON bits as

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

{"some": "json"};type=application/json

but hurl also informs that this request can be run with

curl --header 'Content-Type: multipart/form-data' --form '[email protected];type=application/octet-stream' --form 'data={"some": "json"};type=application/json' 'http://localhost:8000'

which sends

Content-Disposition: form-data; name="data"
Content-Type: application/json

{"some": "json"}

which is a quite funny discrepancy. Shame one cannot exploit it forcing hurl to invoke curl binary instead of using curl library.

fenuks avatar Sep 06 '24 17:09 fenuks