Running the following example is throwing a request body is invalid error.
https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/client/USAGE.md#add-recipients
I'm following the example as mentioned above, but I'm getting the request body is invalid error.
Also, running the same code via API https://sendgrid.com/docs/API_Reference/api_v3.html works perfectly.
I've tried multiple variants, multiple Node environments but couldn't make work this particular endpoint.
Hello @thomasdelgado,
Was there any other data returned with the error? Can you please provide your request body?
Thank you!
With Best Regards,
Elmer
Hi @thinkingserious !
I got same error, I
My request url is https://api.sendgrid.com/v3/contactdb/lists/<
headers2 = {
'authorization': "Bearer " + sg_key
}
method POST
and I GOT response:
{"errors":[{"message":"request body is invalid"}]}
RESPONSE HEADER
{'Date': 'Fri, 11 Jan 2019 14:16:17 GMT', 'Content-Length': '51', 'Content-Type': 'application/json', 'Connection': 'keep-alive', 'Access-Control-Allow-Methods': 'HEAD, GET, PUT, POST, DELETE, OPTIONS, PATCH', 'Server': 'nginx', 'Content-Security-Policy': "default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none'", 'Access-Control-Expose-Headers': 'Link, Location', 'X-Ratelimit-Limit': '3', 'X-Ratelimit-Reset': '1547216178', 'Access-Control-Max-Age': '21600', 'Strict-Transport-Security': 'max-age=31536000', 'Access-Control-Allow-Headers': 'AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha, X-Request-Source', 'X-Ratelimit-Remaining': '1', 'Access-Control-Allow-Origin': '*', 'X-Content-Type-Options': 'nosniff'}
Hello @thinkingserious!
Sorry for late response.
I didn't get any other error, is the same thing as @markszmalc stated. But here's my body function.
export async function addRecipient(email: string, name: string) {
const data = {name: name, email: email}
const request = {
method: 'POST',
url: '/v3/contactdb/recipients',
data: data
};
const result = await sgClient.request(request)
if (result[1].persisted_recipients) {
return result[1].persisted_recipients[0]
} else {
return "-1"
}
}
Since I was in hurry to solve my problem, I've ended up creating a workaround where I call this specific endpoint via plain http request. It would be neat everything using the sdk though.
Thanks!
Hi @thomasdelgado,
Can you please share the plain http request you are using?
Thanks!
With Best Regards,
Elmer
Sure @thinkingserious
This is the solution that I've ended up using for my problem. It uses request 2.88.0
export async function addRecipient(email: string, name: string) {
const data = [{
email : email,
first_name: name }];
const options = {
method: 'POST',
url: 'https://api.sendgrid.com/v3/contactdb/recipients',
headers: { 'content-type': 'application/json',
authorization: 'Bearer ' + key},
body: data,
json: true };
return new Promise((resolve, reject) => {
httpRequest(options, function (error, response, body) {
if (error) {
console.error(error)
throw new Error(error)
}
resolve(body.persisted_recipients)
})
})
}
Thank you!
@thomasdelgado,
Can you try changing const data = {name: name, email: email} with const data = {first_name: name, email: email}? Thanks!
With Best Regards,
Elmer
Hello @thinkingserious!
In that time, I've tried that and lots of variations. (My approach was testing first here https://sendgrid.com/docs/API_Reference/api_v3.html to make sure I was not doing something stupid, then try again on sdk)
I can try that again soon and let you know.
Thanks!
Thanks @thomasdelgado, I appreciate your patience!
This seems to be still an issue!
When I used this code:
const client = require('@sendgrid/client');
client.setApiKey(process.env.SENDGRID_API_KEY);
const data = [
{
"age": 25,
"email": "[email protected]",
"first_name": "",
"last_name": "User"
},
{
"age": 25,
"email": "[email protected]",
"first_name": "Example",
"last_name": "User"
}
];
const request = {};
request.body = data;
request.method = 'POST';
request.url = '/v3/contactdb/recipients';
client.request(request)
.then(([response, body]) => {
console.log(response.statusCode);
console.log(response.body);
})
.catch(error => {
//Log friendly error
console.error(error.toString());
//Extract error msg
const {message, code, response} = error;
//Extract response msg
const {headers, body} = response;
});
I received this response:
201
{ new_count: 0,
updated_count: 0,
error_count: 2,
error_indices: [ 0, 1 ],
unmodified_indices: [],
persisted_recipients: [],
errors:
[ { message: 'string type conversion error',
error_indices: [Array] } ] }
Converting the integer 25 to a string solved the issue.
For future reference: the endpoint should be sent a POST request with a body that contains an ARRAY of new recipients, even if you only send one new recipient.
AKA, change this:
const data = { email : email, first_name: name };
to this
const data = [{ email : email, first_name: name }];