Sendgrid-csharp: Same code one calling from .net Console app and other from .net Web Api and web api fails

Created on 9 Mar 2017  路  4Comments  路  Source: sendgrid/sendgrid-csharp

Web Api Method (Fails)

image

Console Method (Success)

image

  • sendgrid-csharp Version: the latest 9.0.12
  • .NET Version: 4.6

The Console sends the template email.
The Web API returns Status Code Bad Request and the in the Headers a DENY

I used the same api key, template id, and msg params.

Am I totally missing something?

help wanted question

Most helpful comment

Hello @fireole,

After digging a little deeper, I found that we have not considered the case when you pass an empty string to the Content helper when using a template. We will update the SDK to handle this situation. You can follow that progress here: https://github.com/sendgrid/sendgrid-csharp/issues/419

If you do not have any content you wish to pass, then you do not need to call the msg.AddContent method.

With Best Regards,

Elmer

All 4 comments

Hi @fireole,

Could you please tell me the error message? You can grab it like this:

Console.WriteLine(response.Body.ReadAsStringAsync().Result);

Thanks!

so the console app works, but in the web api does not.

so I did this in the web api and captured a screenshot

image

you can see it comes back as BadRequest.

I also put this line in

var test = response.Body.ReadAsStringAsync().Result;

and got this:

{"errors":[{"message":"The content value must be a string at least one character in length.","field":"content.0.value","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.value"}]}

does this help?

got it.

So I was using a template and not really sending any body text for .AddContact(). So I added this line

mailDto.Body = "test" which will fill the msg.AddContent(MimeType.Text, mailDto.Body);

and then it worked.

the msg.AddContent is mandatory whether you use it or not.

Hello @fireole,

After digging a little deeper, I found that we have not considered the case when you pass an empty string to the Content helper when using a template. We will update the SDK to handle this situation. You can follow that progress here: https://github.com/sendgrid/sendgrid-csharp/issues/419

If you do not have any content you wish to pass, then you do not need to call the msg.AddContent method.

With Best Regards,

Elmer

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digime99 picture digime99  路  5Comments

mawiseman picture mawiseman  路  4Comments

ivivanov picture ivivanov  路  3Comments

loganwasif005 picture loganwasif005  路  3Comments

lebamb2 picture lebamb2  路  4Comments