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”

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x