shahid
shahid

Reputation: 79

My flutter Http post request is not sending form-data

Below is my Http post request its not sending my form-data in body and it is working properly in postman. it requires only email parameter in body which i am passing static for now but still,i think my body is sending empty ,any suggestion please.

   Future<Map> getJson() async {
    String apiUrl = 'this is my url';

    Map<String, String> headers = {
      'Content-Type': 'application/json',
      'Authorization':'this is my security token'

    };
    final msg = jsonEncode({
      "email":"[email protected]",
    });

    http.Response response = await http.post(
      apiUrl,
      headers: headers,
      body: msg,
    );
    return json.decode(response.body); // returns a List type
  }

  void check() async{
Map _data = await getJson();
 print("$_data");
  }


  @override
  void initState() {
    super.initState();
    check();
  }

Postman Response

{
    "status": "SUCCESS",
    "msg": {
        "msg": "Password email reset code has been sent in email. Code will get expire after 24 hours!"
    },
    "code": 446167
}

My code Response

{status: ERROR, data: {msg: fields are required}}

Upvotes: 0

Views: 1265

Answers (1)

Nicat
Nicat

Reputation: 203

if you post form-data use this code

var uri = Uri.parse('https://example.com/create');

var request = http.MultipartRequest('POST', uri)
  ..fields['email'] = '[email protected]';

  request.headers.addAll({
      'Content-Type': 'multipart/form-data',
      'Authorization':'your token'
    });


var response = await request.send();
if (response.statusCode == 200) print('Done!');

Upvotes: 1

Related Questions