简体   繁体   中英

SendGrid API returning 400 when I add more JSON properties

I'm trying to use the SendGrid API to create and schedule a Single Send email to a contact list when my Azure Functions endpoint is called. Here is the minimum amount of information that I can create the single send with and have it work with a 200 response.

private static SendGridClient _client = new SendGridClient(
        Environment.GetEnvironmentVariable("SendGridApiKey")
    );

...


var data = new {
            name    = "Title",
            send_to = new {
                list_ids = new [] {
                    $"{Environment.GetEnvironmentVariable("SendGridMailingId")}",
                }
            },
            email_config = new {
                design_id = $"{Environment.GetEnvironmentVariable("SendGridDesignId")}",
            }
        };

        var singleSendResp = await _client.RequestAsync(
            method: SendGridClient.Method.POST,
            urlPath: "marketing/singlesends",
            requestBody: JsonConvert.SerializeObject(data)
        );
        return singleSendResp;

The problem is that I'd like to include the send_at: "now" , suppression_group_id , and sender_id , but if I include any of them (as well as all of them), I get this response:

{
    "errors": [
        {
            "field": "",
            "message": "json could not be unmarshalled"
        }
    ]
}

I've tried all combinations of the above, and have even tried including all the properties that can't be null (minus the subject , html_content , etc since I have design_id .

I'm using the 9.28.1 SendGrid NuGet package, which should correspond to SendGrid's v3 API. I'm doing my testing locally with Postman. I am using the free version of the API, if that matters.

Any help would be appreciated. Thank you!

EDIT: Here would be my ideal object to send, with extra fields.

var data = new {
            name    = "Title",
            send_at = "now",
            send_to = new {
                list_ids = new [] {
                    Environment.GetEnvironmentVariable("SendGridMailingId")
                }
            },
            email_config = new {
                design_id = Environment.GetEnvironmentVariable("SendGridDesignId"),
                sender_id = Environment.GetEnvironmentVariable("SendGridSenderId"),
                suppression_group_id = Environment.GetEnvironmentVariable("SendGridSuppressionId")
            }
        };

I finally figured out the issue, and it's a dumb mistake (as always). sender_id and suppression_group_id are supposed to be integers, but I was just sending the string. Wrapping the values in an int.Parse() works.

Also, even though send_at is supposed to be allowed a value of "now", it only works with the date string. For this, I included the following to format the string

string scheduleDate = DateTime.Now
    .ToUniversalTime()
    .AddMinutes(5)
    .ToString("yyyy-MM-ddTHH:mm:ssZ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM