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 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