user2836288
user2836288

Reputation: 98

Flutter http - Invalid argument(s): Invalid request body when jsonEncode is used

Future<void> _sendData() async {
    final url = Uri.https('websitename.tld', 'restricted/login');
    try {
        final response = await http.post(
          url,
          headers: {"Content-Type": "application/json"},
          body: {
            json.encode({
              'username': 'UserName',
              'userpass': 'UserPaZZw0rd',
            })
          },
        );
 } catch (err) {print(err);}
}

causes : flutter: Invalid argument(s): Invalid request body "{{"username":"UserName","userpass":"UserPaZZw0rd"}}". if i make a call without json, code works like a charm. jsonEncode instead of json.encode -> same result.

Any ideas? Thanks!

Upvotes: 1

Views: 3652

Answers (1)

Jigar Patel
Jigar Patel

Reputation: 5423

Try this.

body: json.encode({
        'username': 'UserName',
        'userpass': 'UserPaZZw0rd',
      }),

Upvotes: 3

Related Questions