I'm working with Azure functions, and I noticed that the request
parameter has the type of an Interface called HttpRequestMessage (see (1)). What exactly does this request
variable contain? Is the HttpRequestMessage
Interface prefix just a recipe for what the request
variable should contain?
@FunctionName("Sales")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
(1) HttpRequestMessage<Optional<String>> request,
@RequestParameterMap
final ExecutionContext context)
This post is somewhat related to a previous post I made: azure function test fails.
I'm working with Azure functions, and I noticed that the request
parameter has the type of an Interface called HttpRequestMessage (see (1)). What exactly does this request
variable contain? Is the HttpRequestMessage
Interface prefix just a recipe for what the request
variable should contain?
@FunctionName("Sales")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
(1) HttpRequestMessage<Optional<String>> request,
@RequestParameterMap
final ExecutionContext context)
This post is somewhat related to a previous post I made: azure function test fails.
As mentioned in MSDOC, the HttpRequestMessage<T>
in Azure Functions represents a request which provides methods and properties to access the details of the HTTP request like:
Content-Type
, Authorization
, etc.,query = request.getQueryParameters().get("name");
RequestUri
property.getBody()
method.name = request.getBody().orElse(query);
Function code:
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
final String query = request.getQueryParameters().get("name");
final String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
}
}
Response:
Functions:
HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
For detailed output, run func with --verbose flag.
[2025-01-22T08:58:16.984Z] Worker process started and initialized.
[2025-01-22T08:58:20.063Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2025-01-22T08:58:22.798Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=20f02fae-1209-4bf5-b49e-e0c2e01e6e73)
[2025-01-22T08:58:23.117Z] Function "HttpExample" (Id: 20f02fae-1209-4bf5-b49e-e0c2e01e6e73) invoked by Java Worker
[2025-01-22T08:58:23.117Z] Java HTTP trigger processed a request.
[2025-01-22T08:58:23.331Z] Executed 'Functions.HttpExample' (Succeeded, Id=20f02fae-1209-4bf5-b49e-e0c2e01e6e73, Duration=590ms)