0

I want to send form data in flutter using the HTTP package. Getting the error:FormatException: Unexpected character (at character 1) I/flutter (30465): I am sending the form data in the HTTP post request.

Future<void> authethicate(
    String schoolName,
    String password,
  ) async {
    try {
      final url = 'https://yobimx.com/citykey/api/users/login';
      final response = await http.post(url, body: {
        'email': '[email protected]',
        'password': '123',
      }, headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      });
      print(
        json.decode(response.body),
      );
      final responseData = json.decode(response.body);
    } catch (error) {
      print(error);
    }
  }
1
  • 1
    if you remove the json.decode and just print response.body, what do you get in the print? Commented Oct 14, 2020 at 6:15

1 Answer 1

3

I have to use a multipart request for request. Thanks for your help.

Future<void> authethicate(
    String schoolName,
    String password,
  ) async {
    try {
      final url = Uri.parse('https://yobimx.com/citykey/api/users/login');
      Map<String, String> requestBody = <String, String>{
        'email': '[email protected]',
        'password': '123'
      };
      var request = http.MultipartRequest('POST', url)
        ..fields.addAll(requestBody);
      var response = await request.send();
      final respStr = await response.stream.bytesToString();
      print(
        jsonDecode(respStr),
      );
      print("This is the Status Code$respStr");
      var encoded = json.decode(respStr);

      print(encoded['status']);
      print('This is the userId${encoded['data']['user_id']}');
    } catch (error) {
      print(error);
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.