0

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

1 Answer 1

1

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!');
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.