I'm having a hell of a time trying to figure out why the same line returns request body as string in Post() and an empty string in Put() and ultimately how to get the request body in Put().
[HttpPost]
public JsonResult Post()
{
...
var todoJson = new StreamReader(Request.InputStream).ReadToEnd();
...
}
[HttpPut]
public JsonResult Put(int id)
{
...
var todoJson = new StreamReader(Request.InputStream).ReadToEnd();
...
}
Based on ((System.Web.HttpInputStream)(Request.InputStream))._data._data
i got in Put(), the byte values are in the request body, however I am failing to extract the content.
Any help greatly appreciated.
Edit: The method from HttpRequest.InputStream documentation works in Post(), in Put() it returns a string "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0..." of Content-Length.
If the request has a Content-Length header, then it has a body. It may be an empty body, but still a body. In contrast to a request with no Content-Length header, which has no body at all, not even an empty one. So yes, a PUT request, technically, strictly, has to have a body.
You can send data to the server in the body of the HTTP PUT request. The type and size of data are not limited. But you must specify the data type in the Content-Type header and the data size in the Content-Length header fields. You can also post data to the server using URL parameters with a PUT request.
I should've seen this earlier Request.InputStream.Position
points to the end of the request body, so
Request.InputStream.Position = 0;
fixes the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With