Send Base64 Encoded Image Using Curl

Send Base64 Encoded Image Using Curl

I am trying to send a base64 encoded image from the command line using curl and base64 like this:

curl -X POST -H "Content-Type: application/json" -d '{"image" : $( base64 ~/Pictures/1.jpg )}'   

However, I get a response back saying that $ is an unexpected token. How do I send the base64 encoded image?

2 Answers

@muru is correct, however if you are trying to send a json encoding your base64 data may be too large for the command line and you may prefer something like this:

(echo -n '{"image": "'; base64 ~/Pictures/1.jpg; echo '"}') |
curl -H "Content-Type: application/json" -d @-   

The -X POST is implied by -d.

4

Bash doesn't expand in single quotes. '{"image" : $( base64 ~/Pictures/1.jpg )}' gets sent as-is. Instead, try:

'{"image" : "'"$( base64 ~/Pictures/1.jpg)"'"}'

(Exit the opening quote before doing command substitution then open a quote again.)

2

Your Answer

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

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.