sendgrid-php: Example code not working: Content-Type should be application/json
Composer:
{
"require": {
"sendgrid/sendgrid": "^6.0"
}
}
On running the hello email example I get following:
(
[0] => HTTP/1.1 415 Unsupported Media Type
[1] => Server: nginx
[2] => Date: Mon, 12 Feb 2018 12:48:00 GMT
[3] => Content-Type: application/json
[4] => Content-Length: 92
[5] => Connection: keep-alive
[6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
[7] => Access-Control-Allow-Methods: POST
[8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
[9] => Access-Control-Max-Age: 600
[10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
[11] =>
[12] =>
)
{"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 17 (7 by maintainers)
@thinkingserious Super, thank you!..
Thanks for clearing that up @asfo!
I’ll leave this issue open so that I can try to reproduce that error using the Without Mail Helper Class example.
@thinkingserious Yes. I have.
Update1: Since everything else looked OK, I thought of creating new SG key. Surprisingly it worked! Hope the issue does not surface again.
Update2: For anyone who stumbles here: the real issue was this- for some reason file_get_contents(“key”) does not work, but trim(file_get_contents(“key”)) works. So yes the key was not proper, but the response did not give any hint about that. Weird.
Final bit from me, the sendgrid php client uses it’s own (not guzzlehttp for example), looking at
php-http-client/lib/Client.phpyou’ll see on line148that it usescurl_init()- so perhaps you could continue your debug down the road of looking into why CURL is failing you?Using the above code i get output:
And in my outlook 365 powered inbox i have this email received within about 5 seconds. Screenshot (proof in the pudding):
I should make it clear that i do not work for sendgrid but have been using them for some time now. As i say, the above code, running on a Ubuntu server, over at Digitalocean using the composer install method and no problems at all. https://gist.github.com/TakesTheBiscuit/422ae14340ec5f9705ccb12370d4d8cd
I wonder if your server is not correctly setup?