cUrl : Argument list too long

I’m getting the error: Argument list too long when trying to use cUrl to send a file in base64 inside the body of my JSON.

I’m using something like this:

DATA=$( base64 "$FILE" )

curl -X POST -H "Content-Type: application/json" -d '{
   "data": "'"$DATA"'"
}' $HOST

Is there any other way to get the DATA in the body of my JSON?

Take into account that I need to read a file in my filesystem, transform it into base64 and then send it inside the body.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If the base64-encoded file is too big to fit in the argument list you are going to have to pass it via a file. One of the easier ways I can think of is to pass it via standard input. From the curl man page, you can use -d @- to read from stdin instead of the command line.

curl -X POST -H "Content-Type: application/json" -d @- "$HOST" <<CURL_DATA
{ "data": "$DATA" }
CURL_DATA

Method 2

Using jo, without having to store the base64-encoded file in a shell variable:

jo data=%"$FILE" |
curl -X POST -H 'Content-Type: application/json' -d @- "$HOST"

This creates a JSON document with a data key. The value for that key will be the base64-encoded contents of the file referenced by the $FILE value.

It’s the % after = in the argument that makes jo base64-encode the file. Had you used @ instead, the file would have been included as a JSON-encoded string.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments