Description
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<java.util.Optional<byte[]>>
- 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
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<Optional<String>> 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<Optional<byte[]>> 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();
}
}