Shahryar Rafique
Shahryar Rafique

Reputation: 1358

how to post form data request using flutter in http package

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);
    }
  }

Upvotes: 0

Views: 7698

Answers (1)

Shahryar Rafique
Shahryar Rafique

Reputation: 1358

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);
    }
  }

Upvotes: 3

Related Questions