2

So, I'm trying to use a service called Postmark to send a formatted HTML email. They have some API documentation here, and they give this example on how to use CURL:

$: curl -X POST "http://api.postmarkapp.com/email" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d" \
-v \
-d "{From: 'sender@example.com', To: 'receiver@example.com', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"

So, that's all good and works just fine when I use my own token. The problem is when I add my own HtmlBody. If I send a simple message, it works just fine. As soon as I add certain special characters, it breaks. For example, if I do something like this:

-d "{From: 'sender@example.com', To: 'receiver@example.com', Subject: 'Postmark test', HtmlBody: '<!DOCTYPE html><html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"

It breaks because of the !. How can I fix this?


UPDATE: As sourcejedi pointed out I am running this from the shell (bash), so the ! issue makes sense to me now. I moved the JSON string to a separate file called email.json and loaded that using -d @email.json. That worked for a simple email with <!DOCTYPE>, but I'm still getting the following error when I try to load the full HTML:

{"ErrorCode":402,"Message":"Received invalid JSON input."}

I believe this has to do with some other special characters. I get the same error when I use --data-urlencode @email.json.

ralphthemagician
  • 219
  • 1
  • 3
  • 12

3 Answers3

3

You need to use --data-urlencode, so it should be like curl -X POST --data-urlencode

When I tested on my end..

* About to connect() to api.postmarkapp.com port 80 (#0)
*   Trying 50.56.54.211... connected
> POST /email HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1
.2.5
> Host: api.postmarkapp.com
> Accept: application/json
> Content-Type: application/json
> X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d
> Content-Length: 164
Ram G
  • 4,821
  • 1
  • 14
  • 26
  • Still seems to break on me an just returns: -bash: !DOCTYPE: event not found – ralphthemagician Aug 28 '12 at 18:48
  • I just tested the same on my end. it works fine.. are you sure that you are receiving the error.. see the test output I got. I also got the application error `Bad or missing server or user API token` as I am using the wrong server token which is expected. – Ram G Aug 28 '12 at 19:02
2

Still seems to break on me an just returns: -bash: !DOCTYPE: event not found

That's an error from the bash shell. You would need to escape the ! as \! yourself. But you'd be better off reading from a file, I think. Instead of -d data use -d @datafile. (Or -d @- to read from stdin).

sourcejedi
  • 2,496
  • 19
  • 35
  • In this case, would I just put the whole JSON string in a file? – ralphthemagician Aug 28 '12 at 20:54
  • 1
    @ralphthemagician Yes, that should work. So long as you don't try to create it from the shell using "echo", because then you would have the exact same problem :). ("cat", or a console-based editor would be fine). – sourcejedi Aug 28 '12 at 21:19
  • I tried it. It works. Sorta. The ! is no longer a problem, but I'm getting "ErrorCode":402,"Message":"Received invalid JSON input." when I try to use the full HTML markup. I think it has something to do with the {} in the style section. Seems to be a step in the right direction though. – ralphthemagician Aug 28 '12 at 22:03
  • 1
    Can't really guess the problem without knowing the input. But it shouldn't be hard to find a way to validate JSON syntax. If one doesn't come to mind, just search & you'll find a web-based validator. – sourcejedi Aug 29 '12 at 08:16
2

I once received the same error when posting JSON data. what I did was enclosed the data with a single quote and use double quote for a string:

so from:

-d "{From: 'sender@example.com', To: 'receiver@example.com', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"

use:

-d '{From: "sender@example.com", To: "receiver@example.com", Subject: "Postmark test", HtmlBody: "<html><body><strong>Hello</strong> dear Postmark user.</body></html>"}'

hope this helps.

Lex Bryan
  • 583
  • 6
  • 13