1

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.

jeffjenx
  • 17,041
  • 6
  • 57
  • 99

1 Answers1

2

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.Parameters.Clear();
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.

Community
  • 1
  • 1
bwest
  • 9,182
  • 3
  • 28
  • 58
  • 1
    Thank you for suggesting that. I had omitted the `request.AddHeader("Content-Type", " multipart/form-data");` and `request.AddHeader("Accept", "application/json");` lines from my code (and the Auth header). But you are providing some different code samples that I will try and see if I can get it to work. Thanks again. – jeffjenx Oct 31 '16 at 17:53
  • 2
    Yes!! It worked!! I used your AddParameter method and hand-crafted my own JSON string as a quick test and it by golly it worked! Thank you! I'm going to use this NewtonSoft thing for JSON, but I will mark this as accepted. – jeffjenx Oct 31 '16 at 18:02
  • Yeah, the important bit is `ParameterType` I think, otherwise those params end up in the querystring – bwest Nov 01 '16 at 18:14