Ubuntu – send base64 encoded image using curl

14.04command linecurl

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 )}' http://some/url/  

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

Best Answer

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.)

Related Question