src/Web/ResponseBuilder.php
public function getFormattedBody() {
        $body = $this->getRawBody();
        $bodyFormatted = null;
        if ($body instanceof StreamInterface) {
            return $body;
        } elseif (isset($this->formatters[$this->format])) {
            $formatter = $this->formatters[$this->format];
            if (!is_object($formatter)) {
                $this->formatters[$this->format] = $formatter = \Reaction::create($formatter);
            }
            if ($formatter instanceof ResponseFormatterInterface) {
                $bodyFormatted = $formatter->format($this);
            } else {
                throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
            }
        } elseif ($this->format === Response::FORMAT_RAW) {
            $bodyFormatted = $body;
        } else {
            throw new InvalidConfigException("Unsupported response format: {$this->format}");
        }

        if (is_array($bodyFormatted)) {
            throw new InvalidArgumentException('Response content must not be an array.');
        } elseif (is_object($bodyFormatted)) {
            if (method_exists($bodyFormatted, '__toString')) {
                $bodyFormatted = $bodyFormatted->__toString();
            } else {
                throw new InvalidArgumentException('Response content must be a string or an object implementing __toString().');
            }
        }
        return $bodyFormatted;
    }
public function getFormattedBody() {
        $body = $this->getRawBody();
        $bodyFormatted = null;
        if ($body instanceof StreamInterface) {
            return $body;
        } elseif (isset($this->formatters[$this->format])) {
            $formatter = $this->formatters[$this->format];
            if (!is_object($formatter)) {
                $this->formatters[$this->format] = $formatter = \Reaction::create($formatter);
            }
            if ($formatter instanceof ResponseFormatterInterface) {
                $bodyFormatted = $formatter->format($this);
            } else {
                throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
            }
        } elseif ($this->format === Response::FORMAT_RAW) {
            $bodyFormatted = $body;
        } else {
            throw new InvalidConfigException("Unsupported response format: {$this->format}");
        }

        if (is_array($bodyFormatted)) {
            throw new InvalidArgumentException('Response content must not be an array.');
        } elseif (is_object($bodyFormatted)) {
            if (method_exists($bodyFormatted, '__toString')) {
                $bodyFormatted = $bodyFormatted->__toString();
            } else {
                throw new InvalidArgumentException('Response content must be a string or an object implementing __toString().');
            }
        }
        return $bodyFormatted;
    }
Get Response body with formatting
@return array|null|StreamInterface|string
@throws InvalidConfigException