简体   繁体   English

如何通过 Postman POST 到 Azure 函数(HTTP 触发器)?

[英]How to POST to an Azure Function (HTTP Trigger) via Postman?

I've implemented an Azure Function, utilizing HTTP Trigger, integrated with SendGrid.我已经实现了一个 Azure 函数,利用 HTTP 触发器,与 SendGrid 集成。 The intended action is to pass data to the Azure Function via HTTP and have that content sent via email to a specified inbox.预期操作是通过 HTTP 将数据传递到 Azure 函数,并将该内容通过电子邮件发送到指定的收件箱。 My Azure Function tests successfully in the Azure Portal.我的 Azure Function 在 Azure 门户中测试成功。 In other words, when I submit this, the inbox receives the expected email:换句话说,当我提交这个时,收件箱会收到预期的电子邮件:

在此处输入图片说明

However, when I attempt to POST to the Azure Function via Postman, I receive status 400 "Bad Request - Invalid Hostname."但是,当我尝试通过 Postman POST 到 Azure 函数时,我收到状态 400“错误的请求 - 无效的主机名”。 I have tried utilizing my function keys, passing the key as a parameter in my URI in Postman and alternatively in the header as "x-functions-key".我尝试使用我的功能键,将键作为参数传递到 Postman 的 URI 中,或者在标题中作为“x-functions-key”传递。 Always status 400. I am getting the URL to POST to from the Azure Portal by clicking "Get Function URL".状态始终为 400。我通过单击“获取函数 URL”从 Azure 门户获取要发布到的 URL。 As an alternative, I've also tried Posting to a URL that conforms to:作为替代方案,我还尝试发布到符合以下条件的 URL:

在此处输入图片说明

Here are my function bindings (function.json):这是我的函数绑定(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"
    }
  ]
}

Here is the function logic (run.csx):这是函数逻辑(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; }
}

How do I POST to the Azure Function via HTTP?如何通过 HTTP POST 到 Azure 函数? How do I resolve the 400 error?如何解决 400 错误? Thank you!谢谢!

For additional information, I am seeking help also via twitter: https://twitter.com/devbogoodski/status/1410702335303581697有关其他信息,我也在通过 twitter 寻求帮助: https : //twitter.com/devbogoodski/status/1410702335303581697

Since you are using a HTTP Trigger make sure your url is in the following format由于您使用的是 HTTP 触发器,请确保您的网址采用以下格式

http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>

Add the following headers:添加以下标题:

x-functions-key: A function-specific API key is required. Default if specific is not provided
Content-Type: application/json

Then add your json object to the body然后将您的 json 对象添加到正文

Ultimately, I made no changes to the Azure Function but instead tried a different tool than Postman to test.最终,我没有对 Azure Function 进行任何更改,而是尝试使用与 Postman 不同的工具进行测试。 I used https://reqbin.com/ .我使用了https://reqbin.com/ Submitted my POST request with the same JSON body I've been using and received status code 200, and the content was sent via email to the specified inbox.使用我一直在使用的相同 JSON 正文提交我的 POST 请求并收到状态代码 200,内容通过电子邮件发送到指定的收件箱。 So, the problem was with Postman - though, at this point, I don't know exactly what.所以,问题出在邮递员身上——不过,在这一点上,我不知道到底是什么。 I've deselected every header option except specifically the ones I intended to use.我已取消选择每个标题选项,特别是我打算使用的选项除外。 And passed my function key via query string in the URL, just as I had did on the successful tests outside Postman, but it never worked in Postman.并通过 URL 中的查询字符串传递我的功能键,就像我在 Postman 之外的成功测试中所做的那样,但它在 Postman 中从未奏效。 So I am not totally sure what is going on.所以我不完全确定发生了什么。 But the Azure Function is working.但是 Azure 函数正在运行。 So I consider this resolved.所以我认为这已经解决了。 Thanks for following along.感谢您的关注。

If you're interested in more about this, I expanded this out a bit here: https://bogoodski.medium.com/setting-up-an-azure-function-sendgrid-http-trigger-cfd9c5791201如果您对此更感兴趣,我在此处对此进行了扩展: https : //bogoodski.medium.com/setting-up-an-azure-function-sendgrid-http-trigger-cfd9c5791201

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

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