question

JerryLipan-2008 avatar image
0 Votes"
JerryLipan-2008 asked JerryLipan-2008 commented

Need help to handle StatusCode:200 and StatusCode:400

This is Web API result. return with Json data and return without Json data

with Json data
109613-200.png


without Json data
109614-400.png



This is the Models

 using System.Collections.Generic;
 using System.Text;
    
 namespace App1Client.Model.Login
 {
     public class User
     {
         public string userId { get; set; }
     }
    
     public class LoginApiResponseModel
     {
         public string authenticationToken { get; set; }
         public User user { get; set; }
     }
 }


This is my code so far,

 public async Task<LoginApiResponseModel> AuthenticateUserAsync(string phonenumber, string password)
         {
             try
             {
                 LoginApiRequestModel loginRequestModel = new LoginApiRequestModel()
                 {
                     Email = phonenumber,
                     Password = password
    
                 };
                 var content = new StringContent(JsonConvert.SerializeObject(loginRequestModel), Encoding.UTF8, "application/json");                
                 var response = await client.PostAsync("api/token", content);
                    
                 if (response.IsSuccessStatusCode)
                 {
                     using (var stream = await response.Content.ReadAsStreamAsync())
                     using (var reader = new StreamReader(stream))
                     using (var json = new JsonTextReader(reader))
                     {
                         var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);
                         Preferences.Set("authToken", jsoncontent.authenticationToken);
                         return jsoncontent;
                     }
                 }
                 else
                 {
                     using (var stream = await response.Content.ReadAsStreamAsync())
                     using (var reader = new StreamReader(stream))
                     using (var json = new JsonTextReader(reader))
                     {
                         var jsoncontent = _serializer.Deserialize<LoginApiResponseModel>(json);
                         Preferences.Set("authToken", jsoncontent.authenticationToken);
                         return jsoncontent;
                     }
                 }
             }
             catch (Exception ex)
             {
                 return null;
             }
         }


If StatusCode=400, I don't know how to return Json with null
109588-500.png


This give an error
109604-600.png

Please help. I'm stuck


dotnet-aspnet-core-mvcdotnet-aspnet-mvc
200.png (71.0 KiB)
400.png (57.3 KiB)
500.png (36.3 KiB)
600.png (12.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JerryLipan-2008 , what's the response body do you want to display?

0 Votes 0 ·

1 Answer

BruceBarker-8516 avatar image
0 Votes"
BruceBarker-8516 answered JerryLipan-2008 commented

A 400 means the parameters are not correct. Typically there is no response body, but if there is, it’s an error message.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Owh. So, how to handle no response body ? Having any code?

0 Votes 0 ·

Owh. So, how to handle no response body ? Having any code?

You have to tell the community how your application is supposed to handle status errors. The current design checks for status errors but assumes every response contains a LoginApiResponseModel. This indicates a bug in the api/token action because it is not returning a LoginApiResponseModel.

The simplest approach, given the current code base, is to return null when there is a status error. The caller can assume the authentication request was not successful.

1 Vote 1 ·

Yesss. Your technical advise is brilliant. I need to change return LoginApiResponseModel into return Status Code

0 Votes 0 ·