有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Azure java函数post方法,发送formdata

我在这里学习了这个教程 functions-create-first-java-maven

除此之外,我正试图做的只是拥有一个post方法,并在请求体中传递表单数据

这是我的函数代码

@FunctionName("create-request")
    public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) final HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");
        context.getLogger().info("RequestBody:- " + request.getBody());
        final int keyIndex = request.getBody().toString().indexOf("request");
        if (keyIndex != -1) {
            final String restString = request.getBody().toString().substring(keyIndex);

            return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + restString).build();

        }
        return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
    }

我试着找邮递员 enter image description here

没有响应,但我的本地控制台显示了大量异常

这似乎是这里的主要问题

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLH77AHEFKOM", Request id "0HLH77AHEFKOM:00000001": An unhandled exception was thrown by the application.
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.create-request ---> Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
Exception: Cannot locate the method signature with the given input
Stack: java.lang.NoSuchMethodException: Cannot locate the method signature with the given input
        at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.lambda$execute$0(JavaMethodExecutor.java:49)
        at java.util.Optional.orElseThrow(Optional.java:290)
        at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:49)
        at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:47)
        at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
        at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
        at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
        at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:91)
        at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

但是,有趣的是,当我传递与x-www-form-urlencoded相同的数据时,它工作得很好

因此,我想知道是否需要修改@HttpTrigger参数并传递一些其他配置来指定函数应该支持的、表单数据以及URL编码方式?因为,我更喜欢将数据作为表单数据传递,而不是作为URL编码传递。 欢迎提出任何建议


共 (1) 个答案

  1. # 1 楼答案

    Httptrigger函数目前似乎不支持form-data内容类型

    我的解决办法是我们可以添加逻辑来处理它

    我们可以从请求头中获取内容类型,然后自己处理该值

    下面是演示代码

    Map<String,String> headers = request.getHeaders();
    if(headers.get("content-type").contains("multipart/form-data"))
    {
        //add logic to handle the value
    }
    else if(headers.get("content-type").contains("application/x-www-form-urlencoded"))
    {
         //add logic to handle the value       
    }