flutter

Send Form Data in HTTP POST request in Flutter

If you want to send form data in HTTP post request in Flutter or Dart, you can use map and add data to it and pass the map variable to the body parameter of http.post() function.

import 'package:http/http.dart' as http;

void main() {
    // This will be sent as form data in the post requst
    var map = new Map<String, dynamic>();
    map['username'] = 'username';
    map['password'] = 'password';

    final response = await http.post(
        Uri.parse('http/url/of/your/api'),
        body: map,
    );

    print(response.body);
}

POST requests in Flutter can be used to send some data to the server using the server API and then get the response of the API to check the working of API. Sometimes the POST API needs to send form data to the server because the post API on the server accepts form data.

To send the form data to the server we have created a Map variable - map and added two key values to it - username and password. We are assigning this map variable to the body parameter of http.post() function. Now the request will be sent to the server along with the form data and the server can use the values of form data keys for further processing on the server.

Was this helpful?