14

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" : "sahpranav1712@gmail.com"
        }
      ],
      "subject" : "Hello, World!"
    }
  ],
  "from" : {
    "email" : "iospranav1712@gmail.com"
  }
}

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!!

iOSer
  • 2,241
  • 1
  • 18
  • 26

6 Answers6

14

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": "mail@gmail.com"
    }],
    "substitutions": {
        "{myFloatVal}": 16.5,
        "{firstname}": "Thomas"
    }
}]
totas
  • 10,288
  • 6
  • 35
  • 32
13

What exactly is a 400 BAD REQUEST?

Why you are getting 400 BAD REQUEST from SendGrid one can only guess. 400 BAD REQUEST means oh we probably 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.

Check for duplicate addresses.

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.

Other Reasons

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. One of the other answers here may help you.

David Sopko
  • 5,263
  • 2
  • 38
  • 42
2

My experience usually is that the body has to be without any new lines (\n or \r for example), otherwise it will return a 400 error. When I clear up any new lines, it works, I usually use something like:

reg_replace('/\s\s+/', ' ', $message)
roy naufal
  • 379
  • 1
  • 8
  • 19
2

While this is an old issue, the following might be helpful for others.

If you are using dynamic templates, make sure that the Id is correct. The dashboard does not lend itself to easy copying of the template id, which means that one can easily include an extra space in front of the id. By adding a space (or any other character for that matter) to the id of the template, SendGrid will simply reply with a Bad Request, without letting you know why.

Kasper
  • 290
  • 1
  • 2
  • 11
  • old but relevant! there is a "d-" in front of the template id which a double-click and copy missed... more detail to copying that template id solved my problem, thank you! – user3398227 Sep 20 '22 at 01:10
1

Not setting [manager setRequestSerializer:[AFJSONRequestSerializer serializer]]; was the issue!!!

iOSer
  • 2,241
  • 1
  • 18
  • 26
  • The highest approved answer seems to describe it better, but this answer would make sense in that it would potentially convert non string values into strings as part of a serialize process. Albeit, I didn't upvote because I was unable to quickly confirm this solution in my code base (didn't see where I could set this information easily). – Arthur Weborg Dec 31 '21 at 04:19
1

Check email list too. If there is null or empty string in your BCC or to list Sendgrid will throw BAD Request.

myworld
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 11 '22 at 12:24
  • Thanks, this was it for me. Crazy that they do not give the reason back in the response... – RogerKint Dec 19 '22 at 09:54