I want to make an post request using curl. The body is a binary file but I also want curl to encode a url-search parameter.
curl --request POST \
--header 'Content-Type:application/octet-stream' \
--data-binary "@file.txt" \ # The body part, raw, not encoded
--data-urlencode "name=john" \ # The url-search-param part, encoded
"https://example.com"
The problem is that curl treats --data-urlencode as part of the body, what I want is that name=john gets appended to the url part like this: https://example.com?name=john whereby the --data-binary part gets send over as the POST body. (in reality the search-parameter is a string with "invalid" url-characters which need to be encoded)
TLDR:
I want to use --data-urlencode as if I'm making a GET request (to append the parameter) and the POST --data-binary to set the actual POST body.
A quick google search gives me the info to use --get / -G but this transforms the request into a GET request what I don't want so there are already a dozen questions about this on SO (but none of them cover my case):
- Is it possible to use `--data-urlencode` and `--data-binary` options for the same curl command? - Different issue, he wants the
--data-binarything to be part of the url (?!). - How to urlencode data for curl command? - Yeah I want that, but also sending a binary file using
POST... - CURL Command Line URL Parameters Well yes, but I want a file as body and It's a
POSTrequest in my case... - How to urlencode url params with a curl POST but not urlencode the body data? - Exactly what I need but again, curl will tread both
--data-urlencodeand--data-binaryas part of the body if-X POSTis used. A look at the comment section of the answer makes me thing that this isn't possible with curl?!
... and many other duplicates of how to just encode the body or url part.