ASP.NET Core request chunking

by on under programming
3 minute read

How to prevent ASP.NET Core sending requests chunked

Basically ASP.NET Core HttpClient sends requests chunked by default. This was not the case for .NET Framework. This can cause some issues for compatibility.

I ran into a problem where a .NET Core library or a .NET Standard library was calling a .NET Framework Web API using HttpClient and the .NET Framework Web API failed to deserialize the request body.

Previously I never had a issue with .NET Framework HttpClient.

Request analysis

.NET Framework

Request

POST http://redacted.redacted.com/redacted/redacted/redacted HTTP/1.1
Authorization: Bearer redacted
Content-Type: application/json; charset=utf-8
Host: redacted.redacted.com
Content-Length: 509
Expect: 100-continue
Connection: Keep-Alive

{"redacted":"redacted","redacted":"redacted","redacted":redacted,"redacted":"redacted"}

Response

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 681
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/redacted
X-AspNet-Version: redacted
X-Powered-By: ASP.NET
Date: Tue, 18 May 2021 11:29:14 GMT

{
  "redacted": redacted,
  "redacted": "redacted",
...

.NET Core

Request

POST http://redacted.redacted.com/redacted/redacted/redacted HTTP/1.1
Authorization: Bearer redacted
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Host: redacted.redacted.com

1FD
{"redacted":"redacted","redacted":"redacted","redacted":redacted,"redacted":"redacted"}
0
  • Notice the Transfer-Encoding: chunked along with 1FD before and 0 after the JSON body.
  • Notce the lack of Content-Length header.

Response

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/redacted
X-AspNet-Version: redacted
X-Powered-By: ASP.NET
Date: Tue, 18 May 2021 11:29:59 GMT
Content-Length: 78

{
  "message": "redacted"
}

Insomnia

Request

POST http://redacted.redacted.com/redacted/redacted/redacted HTTP/1.1
Host: redacted.redacted.com
User-Agent: insomnia/2021.3.0
Connection: Keep-Alive
Authorization: Bearer redacted
Content-Type: application/json
Accept: */*
Content-Length: 564

{
	"redacted": "redacted",
	"redacted": "redacted",
...

Response

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 688
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/redacted
X-AspNet-Version: redacted
X-Powered-By: ASP.NET
Date: Tue, 18 May 2021 20:29:31 GMT

{
  "redacted": redacted,
  "redacted": "redacted",
...

Code fix

Change the following code.

HttpResponseMessage httpResponseMessage = await httpClient.PostAsJsonAsync(apiUrl, obj);

to this.

string str = JsonConvert.SerializeObject(obj);
HttpContent httpContent = new StringContent(str, System.Text.Encoding.UTF8, "application/json");
await httpContent.LoadIntoBufferAsync();
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(apiUrl, httpContent);
comments powered by Disqus