Answered by:
Web api 2.1 does not work with a chunked request

Question
-
User-747114348 posted
I have a application running correctly on web api 2.0 platform.
When I upgrade web api to version 2.1, it does not work.
The client request code such as :
HttpWebRequest hRqst = (HttpWebRequest)WebRequest.Create("http://localhost:7897/api/Test");
hRqst.Method = "Post";
hRqst.ContentType = "application/json";
hRqst.SendChunked = true; // The key code of the problem.
Web api 2.1 would not deserialize the json content, it will deserialize the json data as a null object.
Tuesday, April 1, 2014 9:04 AM
Answers
-
User1570830926 posted
I had the same problem and was able to resolve it by updating this packages
"Microsoft.AspNet.WebApi" => FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.Client" =>FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.Core" => FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.WebHost" => FROM version="5.2.3" TO version="5.2.7"- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 21, 2019 9:13 AM
All replies
-
User1594254982 posted
Can you share the following details?
1. Your controller's action, parameters (if complex type,then its information)
2. Check for ModelState to see if there are any errors.
Tuesday, April 1, 2014 1:44 PM -
User-747114348 posted
Model : Test Object
public class TestObject { public Int32 UserId { get; set; } public Int32 PageIndex { get; set; } public Int32 PageSize { get; set; } }
Controller : Test Controller
public class TestController : ApiController { public TestObject Test(TestObject request) { return request; } }
Reappear step :
1. Create a web api project
2. Upgrade project to .net framework 4.5
3. Upgrade all nuget package
4. Then if you send a chunked request, it does not work.
Tuesday, April 1, 2014 11:28 PM -
User-747114348 posted
If I reinstall the Microsoft.AspNet.WebApi.* nuget package to 5.0.0, it works.
But when those packages upgrade to 5.1 or above, it does not work.
Tuesday, April 1, 2014 11:30 PM -
User401294542 posted
I have the same problem with my ASP.NET Web Api
mangohappy
If I reinstall the Microsoft.AspNet.WebApi.* nuget package to 5.0.0, it works.
But when those packages upgrade to 5.1 or above, it does not work.
I have tried with version <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net45" /> and with version <package id="Ninject.Web.WebApi" version="3.2.1.0" targetFramework="net45" />
but none of these versions seem to handle Chunked Encoded Transfer requests. I also have MVC <package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" /> and <package id="Microsoft.AspNet.Mvc" version="5.2.0" targetFramework="net45" /> but no luck.
If I send an HTTP PUT request with JSON and with Chunked Encoded Transfer (Content-Length is 0), the request reaches the ASP.NET WebApi, the controller redirects it properly to the HttpPost method but the JSON is not de-serialized and the object is NULL. That does not happen if the request is not Chunked Encoded Transfer.
This is my code
[assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace AMS.WebAPI.Controllers { public class SyncController : ApiController { [HttpPut] public HttpStatusCode SendData(int id, int count, [FromBody]MyData records, HttpRequestMessage requestMessage) { var content = requestMessage.Content; string jsonContent = content.ReadAsStringAsync().Result; //this jsonContent has the proper Json return HttpStatusCode.OK; } } }
and this is the class that the framework should "magically" instantiate mapping all the JSON fields to the properties:
namespace AMS.WebAPI.Models { public class MyData { public string NamePerson {get; set;} public int Age {get; set;} public string Color {get; set;} } }
Has anybody found out a way to handle de-serialization of http requests that come chunked transfer encoded in Asp.Net Web Api?
Monday, September 29, 2014 11:35 PM -
User-1832007585 posted
Sounds like real bug. Simple workaround :
public class TestController : ApiController { public async Task<TestObject> Test() { TestObject request = JsonConvert.DeserializeObject<RecordingOptions>(await Request.Content.ReadAsStringAsync()); return request; } }
Monday, October 13, 2014 7:12 PM -
User-598086722 posted
Is this fixed? Or do we have some other workaround to read request to before calling web api framework? Maybe before model binding?
Monday, December 8, 2014 8:09 AM -
User191977431 posted
We are experiencing the same issue, is there any beta version of those nuget packages ?
A github repo to open an issue ?Tuesday, June 27, 2017 9:42 AM -
User-455500886 posted
I had the same issue but I was able to resolve it with the following code:
internal class ChunkedTransferEncodingFix : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Headers.TransferEncodingChunked ?? false) { request.Content.Headers.ContentLength = null; } return base.SendAsync(request, cancellationToken); } }
And then add the following line to the Register method of the WebApiConfig class.
config.MessageHandlers.Add(new ChunkedTransferEncodingFix());
Wednesday, June 6, 2018 9:27 AM -
User1570830926 posted
I had the same problem and was able to resolve it by updating this packages
"Microsoft.AspNet.WebApi" => FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.Client" =>FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.Core" => FROM version="5.2.3" TO version="5.2.7"
"Microsoft.AspNet.WebApi.WebHost" => FROM version="5.2.3" TO version="5.2.7"- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 21, 2019 9:13 AM -
User-11004883 posted
Great call. This was fixed specifically in 5.2.7.
Thanks!
Wednesday, March 27, 2019 7:13 PM