Azure / Azure/azure-functions-java-worker

Java azure function HTTP trigger body is reformatted

未关闭
#291 8 条评论 0 个 reaction 已指派 1 人 已被 @amamounelsayed 认领 在 GitHub 查看
area:java-functions New Feature P1
主要语言
Java
星标
103
派生
74
平均合并
4 天 8 小时
30 天内合并 PR
2

描述

When a HTTP trigger is used the body of the request is reformatted when you use the getBody() method.

There does not seem to be an method to get the raw body either. (which is currently available in the javascript azure functions)

This is causing me an issue with that I am trying to do currently as the format of the message is important to maintain the format it was parsed in.

if the content-type header is not set to application/json it seems to keep the same format as sent in.

Is this a bug or is there any work arounds for this issue i am having?

#### Repro steps

To reproduce this issue make sure the content-type of the request header is application/json and simply send a post request with a json in any irregular format such as below.

```
{"itemA":"valueA",
"itemB":"valueB"}
```

#### Expected behaviour

The expected behaviour is that there should be a way to get the raw body of the post request.
ie read the body of the above example as followed

```
{"itemA":"valueA",
"itemB":"valueB"}
```

#### Actual behavior

There is only access to a formatted json body as shown below.

```
{
"itemA":"valueA",
"itemB":"valueB"
}
```

#### Related information

I was advised that java functions allows the use of byte arrays instead of strings for the request body and this would potentially work but this gave me the exception below

```
System.Private.CoreLib: Exception while executing function: Functions.usingbyte. System.Private.CoreLib: Result: Failure
Exception: ClassCastException: Cannot convert com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource@6dad01aato type com.microsoft.azure.functions.HttpRequestMessage>
```
* Programming language used: Java

below is an example source of both the normal String request and a byte array request which has the other error.

you will see that the log will reformat the message from the getBody request.

Source

```java
package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

public class Function {

@FunctionName("usingstring")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage> request,
final ExecutionContext context) {

context.getLogger().info("Java HTTP trigger processed a request.");

String recievedBody = request.getBody().orElse("");
context.getLogger().info(recievedBody);

return request.createResponseBuilder(HttpStatus.OK).body(recievedBody).build();

}

@FunctionName("usingbyte")
public HttpResponseMessage httpHandler(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request,
final ExecutionContext context) {

context.getLogger().info("Java HTTP trigger processed a request.");

byte[] emptyByteArray = new byte[0];
byte[] recievedBody = request.getBody().orElse(emptyByteArray);
context.getLogger().info(recievedBody.toString());

return request.createResponseBuilder(HttpStatus.OK).body(request.getBody().orElse(emptyByteArray)).build();
}
}
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。