send base64 encoded image using curl
Solution 1
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.)
Solution 2
@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 @- http://some/url/
The -X POST
is implied by -d
.
Related videos on Youtube
lionginass
Updated on September 18, 2022Comments
-
lionginass over 1 year
I need to follow how much money customers owes me. Need to have weekly report.
I take one invoice as example (invoice nr - INV001). I sent invoice on 2020 09 11 (week 37) and received money on 2020 09 28.
I create a code
Select Datepart(Week, [Posting Date]) as week, [Cust_NR], Sum ([Amount]) as Amount from TABLE Where [CUST_NR] = '623772' Group by Datepart(Week, [Posting Date]), [Cust_NR]
Output is simple: week 37 - Amount 1000 EUR (sent invoice on this week), and week 40 - Amount -1000 EUR (received the money)
It is not what I want.
It should be:
week 37 - 1000 EUR (sent an invoice) week 38 - 1000 EUR (money is not received, amount is open) week 39 - 1000 EUR (money is not received, amount is open) week 40 - 0 EUR (money is received in this week and customer owes me nothing)
any tips what should i do? it also enough if someone provides me what to google, because i have no idea.
Thanks
-
meuh almost 9 yearsYou need to double-quote the
$()
else the base64 output lines will be split into several curl arguments. -
muru almost 9 yearsYes, this much better.
-
Moreno about 8 yearsMultiple images files and data command
$ curl -d "name=Avatar&username=avatar&password=A1234567&email=avatar@user.com&token=16b185fe421d5017afb11691681906ed" --data-urlencode 'avatar='"$( base64 ~/Pictures/avatar004.jpg)"'' --data-urlencode 'image='"$( base64 ~/Pictures/avatar001.jpg)"'' --data-urlencode 'picture='"$( base64 ~/Pictures/577383.jpg)"'' http://localhost:8000/api/v1/users/register/
-
Bảo Nam about 6 yearsHi meuh, I failed when I try to run this combined command line from PHP code, $html = shell_exec($str); if I copy $str code and run directly in command line, it's fine. But shell_exec did not work, please help :( thank you so much.
-
Bảo Nam about 6 yearsHaha, I created bash file, chmod +x that file, then write my command to file and run shell_exec('/path to sh file/xxx.sh');, I worked like a charm. Thanks.
-
meuh about 6 years@BảoNam Ok, good solution. I don't much about php, and I don't see any obvious reason why the first version did not work. Look in the error log file of your server for clues. You would be better off starting a new question when php is involved, I think.