Calling ReadAsStreamAsync and then manipulating the resulting stream will change the data that's used by other ReadAs* methods on StreamContent. What's worse is that StreamContent does buffering, so once you call a method like ReadAsStringAsync, the content is effectively 'stuck' in the buffer. Resetting the stream doesn't help.
In this example, post some text like "Hello, World!"
The string that's read from the content is "ello, World!" -- and it's cached in the buffer.
`
public async Task<HttpResponseMessage> PostRequest(HttpRequestMessage request)
{
var stream = await request.Content.ReadAsStreamAsync();
stream.Seek(1L, SeekOrigin.Begin);
var content = await request.Content.ReadAsStringAsync();
stream.Seek(0L, SeekOrigin.Begin);
content = await request.Content.ReadAsStringAsync();
return request.CreateResponse(HttpStatusCode.OK);
}
`
In this example, post some text like "Hello, World!"
The string that's read from the content is "ello, World!" -- and it's cached in the buffer.
`
public async Task<HttpResponseMessage> PostRequest(HttpRequestMessage request)
{
var stream = await request.Content.ReadAsStreamAsync();
stream.Seek(1L, SeekOrigin.Begin);
var content = await request.Content.ReadAsStringAsync();
stream.Seek(0L, SeekOrigin.Begin);
content = await request.Content.ReadAsStringAsync();
return request.CreateResponse(HttpStatusCode.OK);
}
`