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

3

5

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

Hammerfest answered 1/7, 2021 at 21:17 Comment(0)
H
0

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

Hammerfest answered 2/7, 2021 at 3:21 Comment(0)
S
15

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

Samellasameness answered 1/7, 2021 at 21:56 Comment(1)
Thanks. This is a helpful answer. It doesn't resolve the specific error I encountered, but only because I was looking at the wrong place - the function itself - for the source of the error, when the error was actually with the testing software, Postman. Given my original question, this is a helpful answer. So, thank you. Upvoted :)Hammerfest
H
0

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

Hammerfest answered 2/7, 2021 at 3:21 Comment(0)
D
0

I ran into the same issue with a .net core 8.0 (LTS) function. What finally resolved it for me was this:

  1. From the "Overview" page on you Azure Function's page on the Azure Portal
  2. Click on your function class/method in the list of functions
  3. You should now be on the "Code + Test" tab for your function (YourFunctionName | Code + Test)
  4. On the same tab, on the next line, you should see "Get function URL". Click that.
  5. A new blade should open to your right, with three URLS that you can copy.

These URLs add a "code" query parameter with the required value included.

Once I added the "code" query parameter with the value to my Postman request, it was working.

Get Function URL

Dickson answered 29/7 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.