extractBoundary method

  1. @protected
String? extractBoundary(
  1. String contentType
)

Extracts the multipart boundary string from a Content-Type header.

Multipart form-data requests specify a boundary string that separates individual parts in the request body. This method parses the Content-Type header to retrieve that boundary value.

Parameters

  • contentType: The value of the Content-Type header, e.g., "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW".

Returns

The boundary string if found, or null if the header does not contain a boundary parameter.

Notes

  • Quotes around the boundary (e.g., boundary="XYZ") are removed automatically.
  • Only the first boundary parameter is considered.

Example

final contentType = 'multipart/form-data; boundary="abc123"';
final boundary = extractBoundary(contentType);
print(boundary); // Output: abc123

Implementation

@protected
String? extractBoundary(String contentType) {
  final parts = contentType.split(';');

  for (final part in parts) {
    final trimmed = part.trim();
    if (trimmed.startsWith('boundary=')) {
      var boundary = trimmed.substring(9);

      // Remove quotes if present
      if (boundary.startsWith('"') && boundary.endsWith('"')) {
        boundary = boundary.substring(1, boundary.length - 1);
      }

      return boundary;
    }
  }

  return null;
}