description

# postFormData static method

Future<HttpRequest> postFormData(

1. String url,
2. Map<String, String> data, {
3. bool? withCredentials,
4. String? responseType,
5. Map<String, String>? requestHeaders,
6. void onProgress(
   1. ProgressEvent e
   )?,

})

Makes a server POST request with the specified data encoded as form data.

This is roughly the POST equivalent of getString. This method is similar to sending a FormData object with broader browser support but limited to String values.

If `data` is supplied, the key/value pairs are URI encoded with Uri.encodeQueryComponent and converted into an HTTP query string.

Unless otherwise specified, this method appends the following header:

    Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Here's an example of using this method:

    var data = { 'firstName' : 'John', 'lastName' : 'Doe' };
    HttpRequest.postFormData('/send', data).then((HttpRequest resp) {
      // Do something with the response.
    });

See also:

- request

## Implementation

    static Future<HttpRequest> postFormData(
      String url,
      Map<String, String> data, {
      bool? withCredentials,
      String? responseType,
      Map<String, String>? requestHeaders,
      void onProgress(ProgressEvent e)?,
    }) {
      var parts = [];
      data.forEach((key, value) {
        parts.add(
          '${Uri.encodeQueryComponent(key)}='
          '${Uri.encodeQueryComponent(value)}',
        );
      });
      var formData = parts.join('&');

      if (requestHeaders == null) {
        requestHeaders = <String, String>{};
      }
      requestHeaders.putIfAbsent(
        'Content-Type',
        () => 'application/x-www-form-urlencoded; charset=UTF-8',
      );

      return request(
        url,
        method: 'POST',
        withCredentials: withCredentials,
        responseType: responseType,
        requestHeaders: requestHeaders,
        sendData: formData,
        onProgress: onProgress,
      );
    }