Response.json constructor

Response.json(
  1. Object? data, {
  2. int? status,
  3. String? statusText,
  4. Object? headers,
})

Implementation

factory Response.json(
  Object? data, {
  int? status,
  String? statusText,
  Object? headers,
}) {
  final body = utf8.encode(jsonCodec.encode(data));
  final response = Response._(
    Stream<Uint8List>.value(body),
    headers: headers,
    status: status,
    statusText: statusText,
    type: ResponseType.basic,
  );

  // Set headers
  response.headers.set('Content-Type', 'application/json; charset=utf-8');
  response.headers.set('Content-Length', body.length.toString());

  return response;
}