Search code examples
c#asp.netasp.net-web-apihttpclientasp.net-core-3.1

HttpClient not returning NotFound content from WebAPI


Web API Code:

        // POST api/<MAUserController>
        [HttpPost("AuthenticateUser")]
        public async Task<ActionResult<MAUser>> PostAsync([FromHeader] string Email, [FromHeader] string Password)
        {
            string connString = configuration.GetConnectionString("DefaultConnection");
            MAUserVM user = new MAUserVM();
            user = await user.AuthenticateUserAsync(Email, Password, connString);

            if (user.AuthenticationCode == 0)
            {
                return Ok(user._MAUser);
            }
            else if (user.AuthenticationCode == 100)
            {
                return NotFound("Email not found");
            }
            else if (user.AuthenticationCode == 200)
            {
                return NotFound("Incorrect password");
            }
            else
            {
                return NotFound();
            }
        }

Client Code:

using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
                httpClient.DefaultRequestHeaders.Add("Email", Email);
                httpClient.DefaultRequestHeaders.Add("Password", Password);
                using (var response = await httpClient.PostAsync(API_URL, stringContent))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();
                        user = JsonConvert.DeserializeObject<MAUser>(apiResponse);
                    }
                    else
                    {
                        var str = response.StatusCode;
                    }
                }
            }
                return user;

I only get Not Found in var str, but never the associated content - 'Email not found" or "Incorrect Password"


Solution

  • You are not reading the content of the response, only the status code.

    using (var httpClient = new HttpClient()) {
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
        httpClient.DefaultRequestHeaders.Add("Email", Email);
        httpClient.DefaultRequestHeaders.Add("Password", Password);
        using (var response = await httpClient.PostAsync(API_URL, stringContent)) {
            if (response.IsSuccessStatusCode) {
                string apiResponse = await response.Content.ReadAsStringAsync();
                user = JsonConvert.DeserializeObject<MAUser>(apiResponse);
            } else {
                var status = response.StatusCode;
                if (status == HttpStatusCode.NotFound 
                    && response.Content.Headers.ContentLength.GetValueOrDefault() > 0) {
                    string content = await response.Content.ReadAsStringAsync();
                }
            }
        }
    }
    return user;