【发布时间】:2021-07-03 09:07:05
【问题描述】:
我实现了一个 Azure 函数,利用 HTTP 触发器,与 SendGrid 集成。预期的操作是通过 HTTP 将数据传递到 Azure 函数,并通过电子邮件将该内容发送到指定的收件箱。我的 Azure 函数在 Azure 门户中成功测试。换句话说,当我提交这个时,收件箱会收到预期的电子邮件:
但是,当我尝试通过 Postman 发布到 Azure 函数时,我收到状态 400“错误请求 - 无效主机名”。我尝试使用我的功能键,将键作为参数传递到 Postman 的 URI 中,或者在标题中作为“x-functions-key”传递。始终状态为 400。我通过单击“获取函数 URL”从 Azure 门户获取要发布到的 URL。作为替代方案,我还尝试过发布到符合以下条件的 URL:
这是我的函数绑定(function.json):
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"type": "sendGrid",
"name": "$return",
"direction": "out",
"apiKey": "SendGridKey",
"from": "email@email.com",
"to": "email@email.com"
}
]
}
这里是函数逻辑(run.csx):
#r "Newtonsoft.Json"
#r "SendGrid"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
public static SendGridMessage Run(Email req, ILogger log)
{
Guid Id = Guid.NewGuid();
log.LogInformation($"Email generated from websitename via Azure Function. Email ID: {Id}");
SendGridMessage message = new SendGridMessage()
{
Subject = $"From wesbitename. Subj: {req.Subject.Substring(0, 20)}"
};
message.AddContent("text/plain", $"Subject: {req.Subject} \n \n" + $"{req.Content} \n \n" + $"From: {req.CustomerName}, {req.CustomerEmail}");
return message;
}
public class Email
{
public string EmailId { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }
}
如何通过 HTTP POST 到 Azure Function?如何解决 400 错误?谢谢!
有关更多信息,我也在通过 twitter 寻求帮助:https://twitter.com/devbogoodski/status/1410702335303581697
【问题讨论】:
标签: c# azure azure-functions postman