Home > OS >  Response status 301 when making call to web service
Response status 301 when making call to web service

Time:04-26

I need to parse a JSON string receive from a web service.

And this is the function used to get the response.

 Future<void> obtenerUsuario() async {
    setState(() {
      isLoading = true;
    });

    var url = Constantes.flutterAPI   "login_user.php";
    final response = await http.post(Uri.parse(url), body: {
      "email": controlUsuario.text,
      "password": controlContrasena.text
    });

    print("respuesta :"   response.body);

    print("response status code ${response.statusCode}");

    final Map parsed = json.decode(response.body);



    if (parsed.containsKey('email')) {
      print(parsed['email'].toString());

      setState(() {
       // isLoading = false;
      });

   //   Navigator.pushReplacement(context,
     //     MaterialPageRoute(builder: (BuildContext ctx) => HomeScreen()));
    } else {

      setState(() {
      //  isLoading = false;
      });
    }
  }

I am getting following responses printed out:

print("respuesta :"   response.body);

flutter: respuesta :<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.labelconcierge.com/app/flutter_api/login_user.php">here</a>.</p>
</body></html>


print("response status code ${response.statusCode}");

flutter: response status code 301

And when executing the call using Postman, I am getting the output:

{
    "id": "2",
    "email": "[email protected]",
    "imagen": "RHxzlhmifoto.jpeg",
    "nombre": "Modesto",
    "apellidos": "Vasco Fornas",
    "activo": "1",
    "token_firebase": ""
}

I need your help to make it working as it should. Please tell me if the problem lies in the server or in the app code.

CodePudding user response:

Your web service doesn't accept a POST body, it receives parameters via the URL. So your answer would look like this:

  Uri url = Uri.parse('https://www.labelconcierge.com/app/flutter_api/[email protected]&password=12345678');
  await http.post(url).then((response) {
    print("response:"   response.body);
  }).catchError((error) {
    print(error);
  });

And as good practice you should wrap your http calls in then() and catch() functions.

  • Related