我正在使用 Azure 函数,我注意到
request
参数具有名为 HttpRequestMessage 的接口类型(请参阅 (1))。这个 request
变量到底包含什么? HttpRequestMessage
接口前缀只是 request
变量应包含的内容的配方吗?
@FunctionName("Sales")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
(1) HttpRequestMessage<Optional<String>> request,
@RequestParameterMap
final ExecutionContext context)
这篇文章与我之前发表的一篇文章有些相关:azure 功能测试失败。
如 MSDOC 中所述,Azure Functions 中的
HttpRequestMessage<T>
表示一个请求,它提供了访问 HTTP 请求详细信息的方法和属性,例如:
Content-Type
、Authorization
等信息,query = request.getQueryParameters().get("name");
RequestUri
属性访问。getBody()
方法访问。name = request.getBody().orElse(query);
功能代码:
@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();
}
}
回复:
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)