简体   繁体   中英

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

I've implemented an Azure Function, utilizing HTTP Trigger, integrated with 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. My Azure Function tests successfully in the Azure Portal. 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." 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". Always status 400. I am getting the URL to POST to from the Azure Portal by clicking "Get Function URL". As an alternative, I've also tried Posting to a URL that conforms to:

在此处输入图片说明

Here are my function bindings (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):

#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? How do I resolve the 400 error? Thank you!

For additional information, I am seeking help also via twitter: https://twitter.com/devbogoodski/status/1410702335303581697

Since you are using a HTTP Trigger make sure your url is in the following format

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

Ultimately, I made no changes to the Azure Function but instead tried a different tool than Postman to test. I used 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. 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. So I am not totally sure what is going on. But the Azure Function is working. 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

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