Reputation: 123
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
Upvotes: 5
Views: 34406
Reputation: 51
Hope this would help others poking at the answer at this old post. I got the same 400 bad request status with https://reqbin.com too until I add this to the header:
aeg-event-type: Notification
Then it works both in reqbin and Postman. See the details at https://learn.microsoft.com/en-us/azure/communication-services/how-tos/event-grid/local-testing-event-grid#configure-postman
Upvotes: 1
Reputation: 887
I ran into the same issue with a .net core 8.0 (LTS) function. What finally resolved it for me was this:
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.
Upvotes: 0
Reputation: 123
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
Upvotes: 0
Reputation: 874
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
Upvotes: 15