Reputation: 2336
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!!
Upvotes: 17
Views: 31348
Reputation: 311
I found the real error in NodeJs by accessing: error.response.body.errors
try {
const messageObj = {
to: email,
from: CUSTOMER_SERVICE_EMAIL,
templateId,
dynamic_template_data: {}
}
await sgMail.send(messageObj)
} catch (error) {
console.log('error:', error.response.body.errors)
}
Upvotes: 0
Reputation: 1999
You should check the response body for the actual error message. I'm using C# so the following code gave me the JSON response containing the actual error:
var errors = response.Body.ReadAsStringAsync();
In my case, this was the response:
{
"errors": [{
"message": "Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.",
"field": "content",
"help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
}
]
}
Upvotes: 2
Reputation: 41
In my case, it did report what the error was, I just needed to do:
console.error("Error Body:", error.response.body)
And it told me that the categories could not have any non-ASCII characters
Upvotes: 1
Reputation: 31
Check email list too. If there is null or empty string in your BCC or to list Sendgrid will throw BAD Request.
Upvotes: 3
Reputation: 5663
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.
Upvotes: 18
Reputation: 320
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.
Upvotes: 5
Reputation: 399
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)
Upvotes: 3
Reputation: 10800
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"
}
}]
Upvotes: 18
Reputation: 2336
Not setting [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
was the issue!!!
Upvotes: 1