简体   繁体   English

当我添加更多 JSON 属性时,SendGrid API 返回 400

[英]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.当我的 Azure 函数端点被调用时,我正在尝试使用 SendGrid API 创建并安排一个单一发送 email 到联系人列表。 Here is the minimum amount of information that I can create the single send with and have it work with a 200 response.这是我可以创建单个发送并让它与 200 响应一起使用的最少信息量。

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:问题是我想包括send_at: "now"suppression_group_idsender_id ,但如果我包括其中任何一个(以及所有这些),我会收到以下回复:

{
    "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 .我已经尝试了上述所有组合,甚至尝试包括所有不能为null的属性(减去subjecthtml_content等,因为我有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.我使用的是 9.28.1 SendGrid NuGet package,它应该对应于 SendGrid 的 v3 API。我正在使用 Postman 在本地进行测试。如果重要的话,我使用的是 API 的免费版本。

Any help would be appreciated.任何帮助,将不胜感激。 Thank you!谢谢你!

EDIT: Here would be my ideal object to send, with extra fields.编辑:这将是我理想的 object 发送,带有额外的字段。

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. sender_id 和 suppression_group_id 应该是整数,但我只是发送字符串。 Wrapping the values in an int.Parse() works.将值包装在 int.Parse() 中是可行的。

Also, even though send_at is supposed to be allowed a value of "now", it only works with the date string.此外,即使 send_at 应该允许值为“现在”,它也只适用于日期字符串。 For this, I included the following to format the string为此,我包括以下内容来格式化字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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