private function buildRequestBody(RequestInterface $request, array $payload)
    {
        $hasFiles = false;
        foreach ($payload as $value) {
            if ($value instanceof File) {
                $hasFiles = true;
                break;
            }
        }

        if ($hasFiles) {
            $builder = $this->getMultipartStreamBuilder();
            foreach ($payload as $key => $value) {
                if ($value instanceof File) {
                    $builder->addResource(
                        $key,
                        $value->getResource(),
                        ['filename' => $value->getFilename()]
                    );
                } else {
                    $builder->addResource($key, $value);
                }
            }

            try {
                $request = $request
                    ->withHeader(
                        'Content-type',
                        'multipart/form-data; boundary=' . $builder->getBoundary()
                    )
                    ->withBody($builder->build());
            } catch (\Exception $e) {
                throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
            }
        } else {
            try {
                $request = $request
                    ->withHeader('Content-type', 'application/json')
                    ->withBody($this->streamFactory->createStream(json_encode($payload)));
            } catch (\Exception $e) {
                throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
            }
        }

        return $request;
    }