如何通过 Postman POST 到 Azure 函数(HTTP 触发器)?
How to POST to an Azure Function (HTTP Trigger) via Postman?
我已经实现了一个 Azure Function,利用 HTTP 触发器,与 SendGrid 集成。预期的操作是通过 HTTP 将数据传递到 Azure Functions,并通过电子邮件将内容发送到指定的收件箱。我的 Azure Function 在 Azure 门户中测试成功。换句话说,当我提交这个时,收件箱收到预期的电子邮件:
但是,当我尝试通过 Postman POST 到 Azure Functions 时,我收到状态 400“错误请求 - 主机名无效”。我曾尝试使用我的功能键,在 Postman 中将键作为参数传递到我的 URI 中,或者在 header 中作为“x-functions-key”传递。状态始终为 400。我通过单击“获取函数 URL”从 Azure 门户获取 URL 到 POST。作为替代方案,我还尝试过发布到符合以下条件的 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 函数?我该如何解决 400 错误? 谢谢!
有关更多信息,我也在通过 Twitter 寻求帮助:https://twitter.com/devbogoodski/status/1410702335303581697
由于您使用的是 HTTP 触发器,因此请确保您的 url 采用以下格式
http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>
添加以下内容headers:
x-functions-key: A function-specific API key is required. Default if specific is not provided
Content-Type: application/json
然后将你的 json object 添加到 body
最终,我没有对 Azure Function 进行任何更改,而是尝试使用与 Postman 不同的工具进行测试。我用了https://reqbin.com/。使用我一直在使用的 JSON body 提交我的 POST 请求并收到状态代码 200,内容已通过电子邮件发送到指定的收件箱。所以,问题出在邮递员身上——尽管在这一点上,我还不知道到底是什么。我已经取消选择每个 header 选项,除了我打算使用的选项。并通过 URL 中的查询字符串传递了我的功能键,就像我在 Postman 之外的成功测试中所做的那样,但它从未在 Postman 中起作用。所以我不完全确定发生了什么。但 Azure Function 正在运行。所以我认为这已经解决了。感谢您的关注。
如果您对此有更多兴趣,我会在此处进行一些扩展:https://bogoodski.medium.com/setting-up-an-azure-function-sendgrid-http-trigger-cfd9c5791201
我已经实现了一个 Azure Function,利用 HTTP 触发器,与 SendGrid 集成。预期的操作是通过 HTTP 将数据传递到 Azure Functions,并通过电子邮件将内容发送到指定的收件箱。我的 Azure Function 在 Azure 门户中测试成功。换句话说,当我提交这个时,收件箱收到预期的电子邮件:
但是,当我尝试通过 Postman POST 到 Azure Functions 时,我收到状态 400“错误请求 - 主机名无效”。我曾尝试使用我的功能键,在 Postman 中将键作为参数传递到我的 URI 中,或者在 header 中作为“x-functions-key”传递。状态始终为 400。我通过单击“获取函数 URL”从 Azure 门户获取 URL 到 POST。作为替代方案,我还尝试过发布到符合以下条件的 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 函数?我该如何解决 400 错误? 谢谢!
有关更多信息,我也在通过 Twitter 寻求帮助:https://twitter.com/devbogoodski/status/1410702335303581697
由于您使用的是 HTTP 触发器,因此请确保您的 url 采用以下格式
http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>
添加以下内容headers:
x-functions-key: A function-specific API key is required. Default if specific is not provided
Content-Type: application/json
然后将你的 json object 添加到 body
最终,我没有对 Azure Function 进行任何更改,而是尝试使用与 Postman 不同的工具进行测试。我用了https://reqbin.com/。使用我一直在使用的 JSON body 提交我的 POST 请求并收到状态代码 200,内容已通过电子邮件发送到指定的收件箱。所以,问题出在邮递员身上——尽管在这一点上,我还不知道到底是什么。我已经取消选择每个 header 选项,除了我打算使用的选项。并通过 URL 中的查询字符串传递了我的功能键,就像我在 Postman 之外的成功测试中所做的那样,但它从未在 Postman 中起作用。所以我不完全确定发生了什么。但 Azure Function 正在运行。所以我认为这已经解决了。感谢您的关注。
如果您对此有更多兴趣,我会在此处进行一些扩展:https://bogoodski.medium.com/setting-up-an-azure-function-sendgrid-http-trigger-cfd9c5791201