send base64 encoded image using curl

28

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.

Share:
28

Related videos on Youtube

lionginass
Author by

lionginass

Updated on September 18, 2022

Comments

  • lionginass
    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
    meuh almost 9 years
    You need to double-quote the $() else the base64 output lines will be split into several curl arguments.
  • muru
    muru almost 9 years
    Yes, this much better.
  • Moreno
    Moreno about 8 years
    Multiple images files and data command $ curl -d "name=Avatar&username=avatar&password=A1234567&email=avatar@‌​user.com&token=16b18‌​5fe421d5017afb116916‌​81906ed" --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
    Bảo Nam about 6 years
    Hi 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
    Bảo Nam about 6 years
    Haha, 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
    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.