HttpPost does not get ContentLength from PostAsync request
HttpPost does not get ContentLength from PostAsync request
I have a unit test code that sends request to an HttpPost method.
[Test]
public async Task ValidateToken()
{
var content = new FormUrlEncodedContent(new
{
new KeyValuePair<string, string("test", "test")
};
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var response = await httpClient.PostAsync("/api/token/1", content);
Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
}
[Route("api/token")]
public class MyController : BaseApiController
{
private readonly HttpRequest httpRequest;
public MyController(IHttpContextAccessor httpContextAccessor)
{
httpRequest = httpContextAccessor.HttpContext.Request;
}
[HttpPost("{count}")]
public async Task<IActionResult> Post(int count)
{
if(httpRequest.ContentLength)
return BadRequest();
else
return Accepted();
var success= await store.SaveLogStreamAsync(httpRequest.Body, count);
return success ? Accepted() : InternalServerError();
}
}
This unit test always fails because httpRequest.ContentLength
is null. However, when I put a breakpoint on this line and check the ContentLength
of content
, it is always greater than 0.
httpRequest.ContentLength
ContentLength
content
var response = await httpClient.PostAsync("/api/token/1", content);
var response = await httpClient.PostAsync("/api/token/1", content);
Also, when I put a breakpoint before it reaches the Action method, the ContentLength is not 0 anymore. I'm thinking, is there some delay when sending the content?
EDIT:
What I found out is that when testing this from a unit test, the httpRequest.Body
is of type MemoryStream
and httpRequest.ContentLength = null
httpRequest.Body
MemoryStream
httpRequest.ContentLength = null
When the request is sent from Postman, the httpRequest.Body
is of type FrameRequestStream
and httpRequest.ContentLength
always has a value.
httpRequest.Body
FrameRequestStream
httpRequest.ContentLength
@Nkosi the action is awaiting something, I just excluded irrelevant code. and also that's all there is to the routing
– jmc
17 hours ago
There is no [Route] attribute on the controller? If not then the URL you are calling should not work. As for the action, then the code you are showing is misleading as it is incomplete even for a simple example. provide a Minimal, Complete, and Verifiable example that can be used to reproduce the problem.
– Nkosi
17 hours ago
@Nkosi please see edit
– jmc
16 hours ago
1 Answer
1
You can check the request by doing this:
var length= Request.Content.Headers.ContentLength ;
Dunno from where httpRequest
variable is coming
httpRequest
I added the code to see where httpRequest came from
– jmc
25 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you include the controller definition and any attached attributes You route templates look off. Second, that action shown is not awaiting anything so there is no need for it to be async Task.
– Nkosi
17 hours ago