Search code examples
c#azureazure-functionspostman

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:

enter image description here

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:

enter image description here

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 protected]",
      "to": "[email protected]"
    }
  ]
}

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


Solution

  • 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