Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

send base64 encoded image using curl

Writer Emily Wong

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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