Quantastical Quantastical - 2 months ago 30
C# Question

SendGrid POST suppressions: Bad Request

I am trying to create a GUI for managing Email Group Suppressions on SendGrid and am working in a C# .NET environment which I have little experience in.

The documentation shows two endpoints that I need to use for unsubscribing (POST) and re-subscribing (DELETE) to email groups.

I was able to get the DELETE request to work, but the documentation isn't very clear on the POST request, or I just don't understand it.

I've tried this:

var request = new RestRequest("v3/asm/groups/" + group.id + "/suppressions", Method.POST);
request.AddParameter("recipient_emails", "['email@example.com']");

But I get a BadRequest response:

Invalid JSON: invalid character 'r' looking for beginning of value

I also tried these:

var request = new RestRequest("v3/asm/groups/" + group.id + "/suppressions?{\"recipient_emails\":[\"email@example.com\"]}", Method.POST);

var request = new RestRequest("v3/asm/groups/" + group.id + "/suppressions?recipient_emails=['email@example.com']", Method.POST);

But I get a different BadRequest response:

Invalid JSON: unexpected end of JSON input

I understand the error messages, but I am not even sure if what I am doing is correct. Any direction on what I need to do to get a success response?

Also, I am aware there is a C# library provided by SendGrid, but I wasn't able to get it to work. I'm not a C# dev, but I have to do this for work since we're behind. I figure if I can understand a simplified version, I should be able to move on to integrating the library later.

Thank you.


I believe the problem here is that nothing in your request is specifying a content-type which should be application/json. Per this answer, try something like:

request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", strJsonContent, ParameterType.RequestBody);

Where strJsonContent is a JSON string. To construct this string you may want to use something like JSON.NET.