33

I want to send an email with attached pdf file through the Sparkpost API with curl post.

To insert the pdf I use (my test.pdf is ~ 200KB)

"data":"'$(cat test.pdf} | base64 --wrap=0)'"

But somehow this doesn't work out showing the following error:

/usr/bin/curl: Die Argumentliste ist zu lang (original)
/usr/bin/curl: Argument list is too long

EDIT: curl command

curl -X POST https://api.eu.sparkpost.com/api/v1/transmissions -H 'Authorization: <APIKEY>' -H 'Content-Type: application/json' -d '{
   "options":{
      "open_tracking":false,
      "click_tracking":false,
      "inline_css":false
   },
   "recipients":[
      {
         "address":{
            "email":"user@domain.tld",
            "name":"user"
         }
      }
   ],
   "content":{
      "from":{
         "name":"sender",
         "email":"sender@domain.tld"
      },
      "reply_to":"replyto@domain.tld",
      "subject":"subject",
      "text":"textbody",
      "attachments":[
         {
            "name":"attachmentname.pdf",
            "type":"application/pdf",
            "data":"'$(cat test.pdf | base64 --wrap=0)'"
         }
      ]
   }
}'
Simon
  • 373
  • 1
  • 4
  • 6

2 Answers2

70

This is coming up because you are trying to pass the entirety of the base64'd content on the command line. curl has the ability to load in data to POST from a file, which I'd recommend doing. More information can be found in the man page, but the basic format is this:

curl -X POST -d @filename.txt https://website.com/path
Marcus
  • 3,216
  • 2
  • 22
  • 22
  • 8
    A summary on why the `@` usage directly in your answer would be nice, since it's the only missing part without a clear usage indication. – Btc Sources Sep 10 '20 at 13:34
  • Note: Unnecessary use of `-X` ... POST is already inferred. `POST` too [man curl](https://man7.org/linux/man-pages/man1/curl.1.html) – Ax_ Nov 14 '22 at 06:02
  • https://unix.stackexchange.com/questions/144479/what-does-the-at-symbol-before-a-filename-mean-in-a-curl-command – Alwyn Jan 12 '23 at 21:25
  • 2
    To make @Alwyn reference more clear, it points to the explanation of the @ "If you start the data with the letter @, the rest should be a filename" – cre8 Jan 30 '23 at 07:05
2

According to the curl manual, the -F option allows you to encode a file for base64, but limits the output to 76 characters. Ex: -F '=@localfile;encoder=base64'

  • can you givfe full example? – chovy Jun 24 '20 at 19:58
  • Yep! Considering that I am in / home / centos and that sending the names.txt file would use the following command: curl -X POST http://127.0.0.1:1880/test '-F' = @ localfile; encoder = base64 ' However, if the file encoding exceeds 76 characters, the file will be corrupted. – Alisson Mesquita Aug 24 '20 at 19:25