Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendGrid API V3 returns 400 Bad Request

I am trying to achieve send of mail via SendGrid API.

Following is the JSON I am sending as a body of the POST Method

{
  "content" : [
    {
      "type" : "text\/plain",
      "value" : "Hello, World!"
    }
  ],
  "personalizations" : [
    {
      "to" : [
        {
          "email" : "[email protected]"
        }
      ],
      "subject" : "Hello, World!"
    }
  ],
  "from" : {
    "email" : "[email protected]"
  }
}

and following is the return that I am getting

{"message":"Bad Request","field":null,"help":null}

This piece of info doesn't help much.

The authorization is in place, and I believe correctly.

Perhaps, I might have missed some sort of settings in the SendGrid App

If it helps im using Objective-C.

Please help!!

like image 675
iOSer Avatar asked Oct 28 '16 11:10

iOSer


2 Answers

I experienced the same issue and the problem was actually that all substitutions in the personalizations field need to be string values (see https://github.com/sendgrid/sendgrid-php/issues/264)

"personalizations": [{
    "to": [{
        "email": "[email protected]"
    }],
    "substitutions": {
        "{myFloatVal}": 16.5,
        "{firstname}": "Thomas"
    }
}]
like image 74
totas Avatar answered Sep 17 '22 07:09

totas


Why you are getting 400 BAD REQUEST from SendGrid one can only guess. 400 BAD REQUEST means oh we know what happened we are just not telling you.

This is a common 'error handling' 'technique' where the developer catches the error and simply returns false, hiding any of the details or reasons for the failure.

One possible reason, as was in my case, one of the CC addresses was also the TO address. You see among many other things, SendGrid does not allow an email address to occur more than once in a send request, and if you happen to list an email recipient more than once in any of the TO,CC, or BCC lists SendGrid just sends back 400 BAD REQUEST.

There are dozens of other poorly documented reason for 400 BAD REQUEST and I sincerely wish you luck in finding out why SendGrid rejected yours.

like image 38
David Sopko Avatar answered Sep 21 '22 07:09

David Sopko