Forum Xamarin.Android

Status Code 400 Bad Request and Json The Request is Invalid

Hi !
i'm trying to populate a form retreiving information from the database with a Get Method that receives an Int as a parameter from the API, when i try the method in Postman works perfectly, but when i debug the app, it show me 2 errors in different sections and at the end the information came as null because of the same reason.

Error 1:

Error 2:

And here it is the full code:

 BtnGetEstudiante.Click += async delegate
            {
                int GetTelefono = int.Parse(TxtGTelefono.Text);
                EstudianteModel Estudiante = null;
                HttpClient Cliente = new HttpClient();
                string Url = "http://matriculawebapitest.azurewebsites.net/api/Estudiante/GetTelefono";
                var Resultado = await Cliente.GetAsync(Url);
                var json = await Resultado.Content.ReadAsStringAsync();

                try
                {
                    Estudiante = Newtonsoft.Json.JsonConvert.DeserializeObject<EstudianteModel>(json);
                }
                catch (Exception Ex)
                {
                    Toast.MakeText(this, Ex.ToString(), ToastLength.Long).Show();
                }
                if (Estudiante != null)
                {

                    TxtUpNombre.Text = Estudiante.Nombre;
                    TxtUpApellido.Text = Estudiante.Apellidos;
                    TxtUpFechaNacimiento.Text = Estudiante.FechaNacimiento.ToString();
                    TxtUpEnfermedad.Text = Estudiante.Enfermedades;
                    TxtUpCorreo.Text = Estudiante.Correo;
                    TxtUpTelefono.Text = Estudiante.Telefono.ToString();

                }
                else
                {

                    Toast.MakeText(this, json, ToastLength.Short).Show();
                }


            };

Best Answer

  • NeidennXNeidennX Member ✭✭✭
    Accepted Answer

    I just changed the the Response If

                    try
                    {
                        if (Resultado.IsSuccessStatusCode)
                        {
                            Estudiante = Newtonsoft.Json.JsonConvert.DeserializeObject<EstudianteModel>(json);
    
                        }
    
                        if (Estudiante != null)
                        {
    
                            TxtUpNombre.Text = Estudiante.Nombre;
                            TxtUpApellido.Text = Estudiante.Apellidos;
                            TxtUpFechaNacimiento.Text = Estudiante.FechaNacimiento.ToString();
                            TxtUpEnfermedad.Text = Estudiante.Enfermedades;
                            TxtUpCorreo.Text = Estudiante.Correo;
                            TxtUpTelefono.Text = Estudiante.Telefono.ToString();
    
                        }
                        else
                        {
    
                            Toast.MakeText(this, json, ToastLength.Short).Show();
                        }
    

    And its now working
    Thank you all

Answers

  • LeoZhuLeoZhu Member ✭✭✭

    the 400 Bad Request E rror is an Http response status code that indicates that the server was unable to process the request send by client due to invalid syntax. TThe common way you could check your request url and troubleshooting on the server-side. The more ways you could refer to 400 Bad Requst Error

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello all,
    @NeidennX -> check on mobile device with a browser your url if it's return well the information.
    if yes i can't help you
    if no -> check if you have added the internet connection to the manifest of your application

  • NeidennXNeidennX Member ✭✭✭

    @LeoZhu said:
    the 400 Bad Request E rror is an Http response status code that indicates that the server was unable to process the request send by client due to invalid syntax. TThe common way you could check your request url and troubleshooting on the server-side. The more ways you could refer to 400 Bad Requst Error

    Yes i checked the server side and everything is correct with the URL and the syntax, that's why idk what's going on.

  • NeidennXNeidennX Member ✭✭✭

    @ChristopheBERNARD said:
    Hello all,
    @NeidennX -> check on mobile device with a browser your url if it's return well the information.
    if yes i can't help you
    if no -> check if you have added the internet connection to the manifest of your application

    The URL works fine and returns the information well

  • DimChrisDimChris USMember ✭✭✭✭
    edited October 2019

    I don't know if i can help you, cause i don't know exactly what is your coding. I will send you an example how i am using http client

      using (var client = new HttpClient())
      {
             client.Timeout = TimeSpan.FromMilliseconds(20000);
             var response = await client.GetAsync("http://YourApi"));
    
               if (response.IsSuccessStatusCode)
               {
                     string content = await response.Content.ReadAsStringAsync();
                     //In your case
                     var  Estudiante = Newtonsoft.Json.JsonConvert.DeserializeObject<EstudianteModel>(content);
                }
    
         }
    

    In the line if (response.IsSuccessStatusCode), i am checking if status code is 200. In your case try to use the code above and tell me what is your status code

  • DimChrisDimChris USMember ✭✭✭✭

    Also it seems that you are trying to read json string from Get Method. This is wrong cause you don't send any Json string in your code

  • NeidennXNeidennX Member ✭✭✭

    @DimChris said:
    I don't know if i can help you, cause i don't know exactly what is your coding. I will send you an example how i am using http client

      using (var client = new HttpClient())
      {
             client.Timeout = TimeSpan.FromMilliseconds(20000);
             var response = await client.GetAsync("http://YourApi"));
    
               if (response.IsSuccessStatusCode)
               {
                     string content = await response.Content.ReadAsStringAsync();
                     //In your case
                     var  Estudiante = Newtonsoft.Json.JsonConvert.DeserializeObject<EstudianteModel>(content);
                }
    
         }
    

    In the line if (response.IsSuccessStatusCode), i am checking if status code is 200. In your case try to use the code above and tell me what is your status code

    I change the code for the URL and now it's sending the string, but still the same, my Status Code is 200 as well

    The thing is that now it checks the status code and if it is accepted it should enter the if but is not doing it.

  • DimChrisDimChris USMember ✭✭✭✭
    edited October 2019

    What result are you expect in server side? You can pm me for faste help. I think i have catch your exception

  • NeidennXNeidennX Member ✭✭✭
    Accepted Answer

    I just changed the the Response If

                    try
                    {
                        if (Resultado.IsSuccessStatusCode)
                        {
                            Estudiante = Newtonsoft.Json.JsonConvert.DeserializeObject<EstudianteModel>(json);
    
                        }
    
                        if (Estudiante != null)
                        {
    
                            TxtUpNombre.Text = Estudiante.Nombre;
                            TxtUpApellido.Text = Estudiante.Apellidos;
                            TxtUpFechaNacimiento.Text = Estudiante.FechaNacimiento.ToString();
                            TxtUpEnfermedad.Text = Estudiante.Enfermedades;
                            TxtUpCorreo.Text = Estudiante.Correo;
                            TxtUpTelefono.Text = Estudiante.Telefono.ToString();
    
                        }
                        else
                        {
    
                            Toast.MakeText(this, json, ToastLength.Short).Show();
                        }
    

    And its now working
    Thank you all

Sign In or Register to comment.