Simao Gomes Viana
Simao Gomes Viana

Reputation: 631

How to upload a stream using Symfony HTTP Client?

I have a function that sends requests using Symfony HTTP Client, for example, here's what that function uses to send the request:

        $response = $client->request($method, $url, [
            'headers' => [
                'Authorization' => "Bearer $accessToken",
                'Accept' => $acceptContentType,
                'x-ms-client-request-id' => Traceability::getRequestId(),
                ...$headers,
            ],
            'body' => $body,
            'query' => $query,
            'timeout' => 120
        ]);

This function gets passed a $body and I would like to extend this so that it can not only send strings but also binary data, if needed. Now I'm not talking about multipart/form-data but rather just putting the data in the request body like it is required for the Put Blob request in Azure Blob Storage.

Since the data to be sent is not necessarily in a file, but could be any type of resource, I decided to check if it is a resource:

if (is_resource($body)) {

}

What do I need to pass to the request function so that the Symfony client reads from the resource and sends it as request body?

Upvotes: 0

Views: 344

Answers (1)

Simao Gomes Viana
Simao Gomes Viana

Reputation: 631

It turns out you can simply pass the resource as body parameter and it'll handle the rest for you. So the code provided in the question will work just fine, you don't need an extra if statement.

Upvotes: 0

Related Questions