bodyBytes method

List<int> bodyBytes(
  1. Encoding encoding
)

Retrieves the body as a sequence of bytes.

If the source format of the body is a sequence of bytes, it is returned and the encoding is not used.

If the source format of the body is a String, it is converted to a sequence of bytes using the encoder of the encoding.

If there is no source for the body, returns an empty list.

Related methods

Use bodyStr or bodyString to retrieve the body as a String.

If retrieving the bytes to only see if they are empty, use bodyIsEmpty instead (which works more efficiently if the body had been set to a String, since it doesn't need to encode it).

Implementation

List<int> bodyBytes(Encoding encoding) {
  switch (_body) {
    case SimulatedResponseBodyType.none:
      return <int>[]; // empty list of bytes
    case SimulatedResponseBodyType.bytes:
      return _bodyBytes!;
    case SimulatedResponseBodyType.string:
      return encoding.encode(_bodyStr!);
  }
}