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

1,414

Try this.

body: json.encode({
        'username': 'UserName',
        'userpass': 'UserPaZZw0rd',
      }),
Share:
1,414
Author by

user2836288

Updated on December 30, 2022

Comments

  • user2836288 20 minutes
    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!