I checked a lot of documents and blogs, but did not find a perfect solution. I have a solution, it looks a little stupid, but it works.
Solution
You can allow the use of get requests, and then judge it inside Function
. If it is a get
request, manually specify the 405
status code.
The complete sample code is as follows:
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
if (req.Method == HttpMethods.Get) {
var result = new ObjectResult("your message");
result.StatusCode = StatusCodes.Status405MethodNotAllowed;
return result;
}
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
The status code returned is expected: