Azure Java函数发布方法,发送表单数据

时间:2018-10-01 01:26:27

标签: java azure azure-functions serverless

我在这里遵循了本教程 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参数并传递其他配置以指定该功能应支持,表单数据以及urlencoded方式?既然如此,我宁愿将数据作为表单数据传递,而不是像urlencode那样传递。 任何建议都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

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       
}