HttpPayload constructor

HttpPayload([
  1. Object? body,
  2. String? contentType
])

A constructor that wraps HttpPayload.empty, HttpPayload.string, HttpPayload.bytes, HttpPayload.streaming, and HttpPayload.formFields based on the body type.

Use HttpPayload.json to automatically encode the body as a JSON string.

Implementation

factory HttpPayload([Object? body, String? contentType]) {
  if (body == null) {
    return HttpPayload.empty(contentType: contentType);
  }
  if (body is String) {
    return HttpPayload.string(body, contentType: contentType);
  }
  if (body is List<int>) {
    return HttpPayload.bytes(body, contentType: contentType);
  }
  if (body is Stream<List<int>>) {
    return HttpPayload.streaming(body, contentType: contentType);
  }
  if (body is Map<String, String>) {
    return HttpPayload.formFields(body, contentType: contentType);
  }
  throw ArgumentError('Invalid HTTP payload type: ${body.runtimeType}');
}