Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpContent.Read* methods return empty content when using parameter binding in Web API controller #150

Closed
dougbu opened this issue Apr 11, 2018 · 8 comments

Comments

@dougbu
Copy link
Member

dougbu commented Apr 11, 2018

From @naamunds on September 8, 2016 20:55

Repro steps

  1. In a Web API application, add a complex type parameter to a controller method.
  2. In the controller method, read the content, e.g. with Request.Content.ReadAsStringAsync.
  3. Run the app, and send an HTTP request with application/json content to the URI handled by the controller method.

Expected behavior

The content returned by the HttpContent.Read* method represents the actual request's content.

Actual behavior

The content returned by HttpContent.Read* is empty.

Copied from original issue: aspnet/Mvc#5258

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

From @naamunds on September 8, 2016 21:4

This StackOverflow answer suggests that in Web API the request content can only be read once by design, but I was able to successfully invoke the Request.Content.Read* methods multiple times within a controller method when not using parameter binding, so it appears that the issue is specific to parameter binding.

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

From @davidfowl on September 12, 2016 6:20

This doesn't have anything to do with ASP.NET Core does it?

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

From @naamunds on September 12, 2016 16:32

@davidfowl I encountered this issue in ASP.NET Web API 2.x (not Core). Would https://github.com/aspnet/AspNetWebStack instead be the appropriate place to log this issue?

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

That repo is not yet public -- we're working on the infrastructure. File issues at https://aspnetwebstack.codeplex.com/ until that changes.

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

From @naamunds on September 12, 2016 19:36

I see. I've logged https://aspnetwebstack.codeplex.com/workitem/2315 for this issue.

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

From @mahmoud690 on April 11, 2018 11:53

I have the same error. I use the latest version of WebApi (5.2.4) but the problem still exists.

@dougbu
Copy link
Member Author

dougbu commented Apr 11, 2018

Reopened in preparation for move to aspnet/AspNetWebStack

Side note: CodePlex is now in archived state. We never saw https://aspnetwebstack.codeplex.com/workitem/2315

@dougbu
Copy link
Member Author

dougbu commented Apr 17, 2018

Any Web API parameter that is a complex type is automatically bound from that request body. That consumes the body, leaving the stream positioned at the end. Request.Content.ReadAsStringAsync() thus returns no content.

It may be possible to rewind the request stream. Your application will probably need to call IHostBufferPolicySelector.UseBufferedInputStream(object) to enable rewinding (though it should be enabled already when running in IIS or Owin). But, if not, this issue is by design.

Either way @mahmoud690 and @naamunds we thank you for your feedback. We're closing this issue as the behaviour discussed is by design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant