send base64 encoded image using curl
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.
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