gpt4 book ai didi

azure - 在使用JAVA的azure函数中,如何获取请求正文

转载 作者:行者123 更新时间:2023-12-02 03:34:10 25 4
gpt4 key购买 nike

在使用JAVA的azure函数中如何获取以json形式发送的请求负载

我的代码是这样的:

@FunctionName("hello")
public HttpResponseMessage<String> hello(
@HttpTrigger(name = "req", methods = {"post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,final ExecutionContext context) throws Exception {
context.getLogger().info("Java HTTP trigger processed a request.");
System.out.println("**********REQUEST BODY*************"+request.getBody());

String cookie = new Authenticate().authenticateAndGetCookie();
ExecuteCommands commandsExecutor = new ExecuteCommands(cookie);
String s=commandsExecutor.control(request.toString());
System.out.println(s);

JSONObject jobj=new JSONObject(s);


if (s == null) {
return request.createResponse(400, "Please pass a name on the query string or in the request body");
} else {
return request.createResponse(200,jobj.toString());
}
}

由于使用了 HttpRequestMessage>,当我在控制台上打印正文(如我上面粘贴的代码中所示)时,我会在控制台中获取此内容。

**********REQUEST BODY*************Optional[{
[13-06-2018 11:40:18] Java HTTP trigger processed a request.
[13-06-2018 11:40:18] "Command": "com",
[13-06-2018 11:40:18] "Id": "id",
[13-06-2018 11:40:18] "Id2": "id2",
[13-06-2018 11:40:18] "Operation": "op"
[13-06-2018 11:40:18] }]

但是我传递了以下有效负载:

{
"Command": "com",
"Id": "id",
"Id2": "id2",
"Operation": "op"
}

所以,我尝试使用 HttpRequestMessage 但出现以下异常

incompatible types: com.microsoft.azure.serverless.functions.HttpRequestMessage<java.util.Optional<java.lang.String>> cannot be converted to com.microsoft.azure.serverless.functions.HttpRequestMessage<java.lang.String>

最佳答案

我想你可以这样做

String body = request.getBody().orElse("");

查看完整示例 this article .

关于azure - 在使用JAVA的azure函数中,如何获取请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50836488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com