I'm attempting to use cURL to POST form data to an API. The API's documentation states that it accepts multipart/form-data content types.

Since this is part of a script, I'm trying to build a semicolon-delimited string of form fields, which is documented here in cURL's documentation.

What I've Tried

What I'd like to be able to do (in order to easily build this string based on arguments passed to the script) is this:

local _payload="file=@file;username=name;content=hello"
echo "INFO payload: ${_payload}"
curl -i -F "${_payload}" "${url}"

$ ./myscript.sh <args>
# INFO payload: file=@file;username=name;content=hello  -- my script
# Warning: skip unknown form field: username=name       -- cURL
# Warning: skip unknown form field: content=hello       -- cURL

I get those warnings from cURL, and as expected the API never gets those values.

What does work is passing each field as its own argument to cURL, i.e.

curl -i -F "file=@file" -F "username=name" -F "content=hello" "${url}"

...but this isn't as nice to account for programmatically.

I should edit to mention that, in both cases, the file is received and processed properly.


I'm certain that this is something that I'm missing, but after poring over the documentation for cURL and the API I'm requesting, I can't find it. Why is cURL skipping these form fields, and how can I prevent/fix it?


I have made a breakthrough, and of course it's right after I post my first question to Unix.SE. I re-read the docs and realized (somehow for the first time) that I'm improperly using the semicolon-delimited string.

The docs state that the semicolon is used not to separate multiple fields, but multiple qualities of the same field. There's where I went wrong.

The API docs state that endpoints accept a url-encoded JSON payload as a payload_json field, so I'm instead doing this:

local _json="payload_json=$(build_json)"  # defined elsewhere
local _file="file=@file"
curl -i -F "${_file}" -F "${_json}" "${url}"

and lo, it works. RTFD!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.