0

Using Azure Functions .Net 5 and the Microsoft.Azure.Functions.Worker.Http SDK, what is the expected behaviour of HttpRequestData.Body property when the request has no body? Is it null, is it a Stream.Empty, something else?

  • The documentation is unhelpful, as it simply says: "A Stream containing the HTTP body data."
  • Azure Functions Core 5 were released this year, there is little information available.
  • There is information on how to read body requests with ASP Core 3 but this is not .NET 5, and I am not sure Azure Functions are expected to behave in the same way as ASP.
  • I was so far unable to attach the debugger to the published Function Host, which complicates diagnostics.
E_net4 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
Ama
  • 1,133
  • 7
  • 22

1 Answers1

0

what is the expected behaviour of HttpRequestData.Body property when the request has no body?

The raw HTTP request body as a bytestring. This is useful for processing data in different ways like loading the data, parsing etc. The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers. If content-length is nothing, then the request read return nothing to read. Sometimes throws errors if the EOF (Function) return type is not valid.

To read http request body from asp.net core 5 using azure functions

  1. Create Asp.Net Core 5 Project and Azure Functions isolated environment
  2. Add each of their references in packages/dependencies. You need to change the target framework of Microsoft SDK version from 3.1 to 5 because by default it is create in 3.1 in Azure Functions csproj.

And We need to do changes in the local.setting.json file as well as Program.cs file in Core project for reading the Http Request Body. Refer this and this for step by step process!


In HttpRequestBody, Content-length : some numeric value (but not zero) specifies the end of the HttpRequest and its length. If Content-Length: 0, then HttpRequestData contains empty body like { }.

if neither ("Content-Length" / "Transfer-Encoding: chunked") are present, "the end of the connection" signals the end of the request.

Most HTTP requests are GET requests without bodies. However, simulating requests with bodies is important to properly stress the proxy code and to test various hooks working with such requests. Most HTTP requests with bodies use POST or PUT request method.

For more information on HttpRequestBody - Refer this

HariKrishnaRajoli-MT
  • 3,193
  • 1
  • 4
  • 18
  • Thanks. So is it correct to understand that `HttpRequestData.Body` will never return null, but that the stream may be empty? And the only way to check for this is to check for the presence of the Content-Length header? – Ama Sep 28 '21 at 17:57
  • Technically, If the request had included a `Content-Length: 0` header, that would be an empty body. An Empty body of `HttpRequestData` will return empty object or nothing. – HariKrishnaRajoli-MT Sep 29 '21 at 00:42
  • Where did you get this information from? By empty object, do you mean `Stream.Empty` ? – Ama Sep 29 '21 at 16:39
  • @Ama, I got this information from my learning - HttpRequestsAndResponse. In ***HttpRequestBody***, `Content-length` : some numeric value (but not zero) specifies the end of the HttpRequest and its length. If `Content-Length: 0`, then ***HttpRequestData*** contains empty body like { }. if neither ("`Content-Length`" / "`Transfer-Encoding: chunked`") are present, "the end of the connection" signals the end of the request. – HariKrishnaRajoli-MT Sep 29 '21 at 17:32
  • Most HTTP requests are GET requests without bodies. However, simulating requests with bodies is important to properly stress the proxy code and to test various hooks working with such requests. Most HTTP requests with bodies use POST or PUT request method. – HariKrishnaRajoli-MT Sep 29 '21 at 17:32
  • For more information on HttpRequestBody - Refer [this](https://fastapi.tiangolo.com/tutorial/body/#request-body) – HariKrishnaRajoli-MT Sep 29 '21 at 17:33
  • If you have any doubt, please do let me know, I'll try to learn and clarify – HariKrishnaRajoli-MT Sep 29 '21 at 17:34
  • There must be a misunderstanding. My question relates to Azure Function's implementation of `Stream HttpRequestData.Body`; you are answering about implementation details of the HTTP protocol, which to my understanding is off-topic here. Also, your link refers to python. – Ama Oct 01 '21 at 13:00