Sendgrid-php: Example code not working: Content-Type should be application/json

Created on 12 Feb 2018  路  17Comments  路  Source: sendgrid/sendgrid-php

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}]}
unknown or a help wanted bug

All 17 comments

Hello @kargirwar,

Do you mind providing the exact source code you used that generated that error so that I can try to reproduce?

Thanks!

With Best Regards,

Elmer

Just this:

```
// If you are using Composer (recommended)
require 'vendor/autoload.php';

// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email("Example User", "[email protected]");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "[email protected]");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

//$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid(file_get_contents("key"));

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
````

@kargirwar Couple of suggestions for you:

  1. What is:
    file_get_contents("key") ?
    I suspect that you don't actually have a file called 'key' on your filesystem? in which case change it to
    $sg = new \SendGrid("YOUR_API_KEY_HERE");
    line 14/15 area.

  2. I think your name spacing is a bit 'odd'.
    If (1) doesn't work, Maybe try this:

<?php
// If you are using Composer (recommended)
require 'vendor/autoload.php';

// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");

$from = new \SendGrid\Email("Example User", "[email protected]");
$subject = "Sending with SendGrid is Fun";
$to = new \SendGrid\Email("Example User", "[email protected]");
$content = new \SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new \SendGrid\Mail($from, $subject, $to, $content);

//$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid("YOUR_API_KEY");

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();

I don't know the context of your code otherwise i'd suggest use Sendgrid; at the top rather than absolute naming them like i have in the example above

Hope this helps!

Were you able to reproduce the problem ? I can reproduce even now with your code.

I copied the code straight from the github readme . Only change was that instead of setting the environment variable I read the key from a file. In any case the response does not say that the key is not valid.

Using the above code i get output:

聽 202 Array
聽 | (
聽 | [0] => HTTP/1.1 202 Accepted
聽 | [1] => Server: nginx
聽 | [2] => Date: Fri, 16 Feb 2018 13:46:24 GMT
聽 | [3] => Content-Type: text/plain; charset=utf-8
聽 | [4] => Content-Length: 0
聽 | [5] => Connection: keep-alive
聽 | [6] => X-Message-Id: BjCL9iZmT_2uzh9fuDYDzg
聽 | [7] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
聽 | [8] => Access-Control-Allow-Methods: POST
聽 | [9] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
聽 | [10] => Access-Control-Max-Age: 600
聽 | [11] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
聽 | [12] =>
聽 | [13] =>
聽 | )
聽 | 聽

And in my outlook 365 powered inbox i have this email received within about 5 seconds.
Screenshot (proof in the pudding):

screen shot 2018-02-16 at 13 48 43

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

"sendgrid/sendgrid": "^6.0",

I wonder if your server is not correctly setup?

Final bit from me, the sendgrid php client uses it's own (not guzzlehttp for example), looking at php-http-client/lib/Client.php you'll see on line 148 that it uses curl_init() - so perhaps you could continue your debug down the road of looking into why CURL is failing you?

Thanks @TakesTheBiscuit,

I think that is a reasonable suggestion. I do recall other customers having issues where there server was not configured to enable CURL support by default.

@kargirwar,

Have you checked your server configuration to ensure CURL support is enabled?

@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.

Thanks for following up @kargirwar!

I'm having this issue too...

The key change doesn't works for me...also, I'm not using "getenv()" instead I use my key in a string...is that bad way to do it? (I just checked and everything seems right to the example you include at the README)

Hello @asfo,

We advise against including your key as a string in the code because that increases the chance a bad actor may obtain your API key.

To further help:

  1. How did you install the SDK?
  2. What version of the SDK are you using?
  3. What version of PHP are you using?
  4. Can you provide any details regarding the server you are using?
  5. Can you verify your key is valid by trying a sample call here?

Thanks!

With Best Regards,

Elmer

Thanks for your answer @thinkingserious

I know, but I was just testing the library in a script.

1.- I installed it using Composer
2.- Lastes
3.- 7.2 (also tested on 7.0 and 5.6)
4.- Nginx + PHP-FPM in the versions above running inside a Docker Container with bot containers using Alpine and running on a server with Debian.
5.- Yes, we already have this key working on another script in NodeJS

Also, I need to add that I used the second example, and this is not working at all with the same issue the OP included.

But if I change to the first example, it works ...I don't know why I just did a simple "hello.php" file, copy the example and even with the .env (which I tested too) doesn't works, only with the first example works.

PS:
By second example I reference to this:
Without Mail Helper Class <--
And by first example:
Hello Email <--

Thanks!

Hello @asfo,

If I'm understanding correctly, your issue is resolved by using the first example? If not, could you please provide the source code that is broken so that I can try to reproduce?

Thanks!

With Best Regards,

Elmer

@thinkingserious yes it got fixed using the first example.

So technically the SDK works but the second example is wrong, so I don't know if is an internal issue (from the SDK) or the example is wrong.

Hope that helps.

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 Super, thank you!...

Hi @asfo,

I believe this issue to be resolved in version v7.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rainman0607 picture rainman0607  路  4Comments

rsalunga29 picture rsalunga29  路  4Comments

solonifer picture solonifer  路  3Comments

Aubynj picture Aubynj  路  3Comments

KayakinKoder picture KayakinKoder  路  5Comments