0

This is my register method inside the AuthController.

[HttpPost(ApiRoutes.Auth.Register)]
public async Task<IActionResult> Register(UserRegistrationRequest request)
{
    var authResponse = await _authService.RegisterAsync(request.Email, request.Password);

    if (!authResponse.Success)
    {
        return BadRequest(new AuthFailedResponse
        {
            Errors = authResponse.Errors
        });
    }

    return Ok(new AuthSuccessResponse
    {
        Token = authResponse.Token,
        RefreshToken = authResponse.RefreshToken
    });
}

I'm trying to call this method by using TestClient.PostAsync() method, unfortunately it always returns Bad Request. I've already tried calling the TestClient.PostAsJsonAsync(ApiRoutes.Auth.Register, user) method by importing Microsoft.AspNet.WebApi.Client package, the result is the same.

var user = new UserRegistrationRequest
    {
        Email = "user1@testtest.com",
        Password = "P@ssw0rd1!!!!!"
    };

var response = await TestClient.PostAsync(
        ApiRoutes.Auth.Register,
        new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8)
        {
            Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
        });

Error details

Zabavsky
  • 13,340
  • 8
  • 54
  • 79
cannelle28
  • 168
  • 5
  • 17
  • It's not clear from the info you provide if that 400 is coming from your own call to BadRequest if the authResponse is not succesful, or if you have the [ApiController] attribute set and it's doing auto model validation and returning a 400 before it even hits your method. If you debug this, does it even hit your call to RegisterAsync()? – Bryan Lewis Apr 08 '20 at 17:04
  • I investigated it more and you are correct, it's not even hitting RegisterAsync(). i've noticed a "Failed to determine the https port for redirect integration test" warning in output window so I've fixed it by setting the BaseAddress property of HttpClient, the warning disappeared, the test is still not working. What bothers me is that this controller works as expected in swagger and postman. ALSO I can test other get and post calls in other controllers using GetAsync() and PostAsAsync() methods. – cannelle28 Apr 09 '20 at 15:34
  • You don't provide your implementation of UserRegistrationRequest, but can we assume that it only has the two properties you show (Email & Password)? It's not missing anything that would cause the 400? Also, are you in fact using the [ApiController] attribute on this controller? – Bryan Lewis Apr 09 '20 at 15:45
  • Yes, it has only Email and Password. I'm wondering if the problem is related to wrong object serialization as it is in this thread [https://stackoverflow.com/questions/33680203/httpclient-postasjsonasync)](https://stackoverflow.com/questions/33680203/httpclient-postasjsonasync) – cannelle28 Apr 09 '20 at 15:49

1 Answers1

0

You are missing the FromBody attribute from you action parameter. When you are sending json data to a controller that will be part of the request body. You can tell to the controller how to bind the incoming data, in your case from the body. So you code should look like:

public async Task<IActionResult> Register([FromBody]UserRegistrationRequest request)
{
    …
}

You could read more about bindings in the official documentation.

gpro
  • 579
  • 8
  • 17