Sendgrid-php: Getting 415 error when sending via the API

Created on 22 Mar 2017  路  3Comments  路  Source: sendgrid/sendgrid-php

Issue Summary

If I do the hello php code snippet with my API key, the email send without issue, but when I try and do this from within my application, I consistently get a 415 error. I've done headers_list() throughout the code and it always returns an empty array, so something else must be causing it to think it has the wrong header?

array (
  0 => 'HTTP/1.1 415 Unsupported Media Type
',
  1 => 'Server: nginx
',
  2 => 'Date: Wed, 22 Mar 2017 22:03:15 GMT
',
  3 => 'Content-Type: application/json
',
  4 => 'Content-Length: 92
',
  5 => 'Connection: keep-alive
',
  6 => 'X-Frame-Options: DENY
',
  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 => '',
)
'{"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}'

Technical details:

  • sendgrid-php Version: 5.4
  • PHP Version: 7.0

Most helpful comment

If anyone else is having this issue, make sure your API key doesn't (for whatever reason) have linebreaks at the end of it. In my case, I was reading creds from a repository-excluded file to test if my wrapper _actually_ worked, and was puzzled when I kept getting a 415.


If you were to have a file like this:

.sendgrid: (notice the linebreak)

SG.**

and you used it like

$apiKey = file_get_contents('.sendgrid');
$sendgrid = new \SendGrid($apiKey);
$sendgrid->client->mail()->send()->post($message);

The raw HTTP request, sent by curl, would look like: (notice the linebreak)

POST /v3/mail/send
Host: api.sendgrid.com
Authorization: Bearer SG.**

User-Agent: sendgrid/*;php
Accept: application/json
Content-Type: application/json

This causes all the headers after the Authorization header to be treated as the message body and effectively ignored. A simple pass through trim will fix this: $apiKey = trim($apiKey);

Hope I save someone a headache :smile:

All 3 comments

Sorry ... this was coder-error...

If anyone else is having this issue, make sure your API key doesn't (for whatever reason) have linebreaks at the end of it. In my case, I was reading creds from a repository-excluded file to test if my wrapper _actually_ worked, and was puzzled when I kept getting a 415.


If you were to have a file like this:

.sendgrid: (notice the linebreak)

SG.**

and you used it like

$apiKey = file_get_contents('.sendgrid');
$sendgrid = new \SendGrid($apiKey);
$sendgrid->client->mail()->send()->post($message);

The raw HTTP request, sent by curl, would look like: (notice the linebreak)

POST /v3/mail/send
Host: api.sendgrid.com
Authorization: Bearer SG.**

User-Agent: sendgrid/*;php
Accept: application/json
Content-Type: application/json

This causes all the headers after the Authorization header to be treated as the message body and effectively ignored. A simple pass through trim will fix this: $apiKey = trim($apiKey);

Hope I save someone a headache :smile:

Hi @AlbinoDrought,

Would you mind adding the trim fix to our docs via PR?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

morazain picture morazain  路  3Comments

rainman0607 picture rainman0607  路  4Comments

moontrv picture moontrv  路  3Comments

jverlee picture jverlee  路  4Comments

solonifer picture solonifer  路  3Comments