php-http / httplug

HTTPlug, the HTTP client abstraction for PHP

Home Page:http://httplug.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sending file from a multipart stream

BenoitLeveque opened this issue · comments

Q A
Bug? yes
New Feature? no
Version v1.1.0
PHP Version 7.1.5

Actual Behavior

I'm sending some files with a multipart api endpoint, i've followed the doc (http://php-http.readthedocs.io/en/latest/components/multipart-stream-builder.html).

When the profiler is enabled i got this error Error: escapeshellarg(): Input string contains NULL bytes from the CurlCommandFormatter.php

Expected Behavior

no error

Steps to Reproduce

i've followed the documentation here : http://php-http.readthedocs.io/en/latest/components/multipart-stream-builder.html

Can you show us some minimal code here and also how you run it?

Also, do you have the involved CurlCommandFormatter.php line?

$request = $this->messageFactory->createRequest('POST', 'api_url');

$data = file_get_contents('foo.jpeg'); // content of the file
$builder = new MultipartStreamBuilder($this->streamFactory);
$builder->addResource('file', $data, ['headers' => ['Content-Type' => $file->getMimeType()], 'filename' => $file->getFilename()]);

$multipartStream = $builder->build();
$boundary = $builder->getBoundary();

$request = $request->withBody($multipartStream);
$request = $request->withHeader('Content-Type', 'multipart/form-data; boundary="'.$boundary.'"');

$response = $this->client->sendRequest($request);

You can put this code inside a symfony controller you should have the error.

You are using the CurlClient. It is super fast but also very strict. You have forgotten to add a content-length header. You could use the Content-Length plugin to fix this.

The issue is about using the CurlCommandFormatter so it should happen even when not sending the request. I tried to reproduce it locally but it's working.
Which stream implementation are you using? zend, slim or guzzle?

Guzzle psr7.

I tried the code with curl - fail
Tried with guzzle - success
Tried with curl and content-length plugin - success.

But I might be wrong.

I tried without Symfony and without any client. It worked! I also have the Content-Length header set by the MultipartStreamBuilder.

I'm using guzzle with the symfony integration

here is my configuration :

httplug:
    classes:
        client: Http\Adapter\Guzzle6\Client
        message_factory: Http\Message\MessageFactory\GuzzleMessageFactory
        uri_factory: Http\Message\UriFactory\GuzzleUriFactory
        stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory

each of my clients use factory: 'httplug.factory.guzzle6'

My issue is only present when the profiler is enabled, the ProfilePlugin use a Formatter that use CurlCommandFormatter.

for a quick workaround i've disable the profile plugin and everything work fine

Which plugins are you using?

I'm using 3 plugins:

  • 'httplug.plugin.logger'
  • reference:
    id: 'app.httplug.plugin.base_uri'
  • 'httplug.plugin.authentication.customer'

This has been fixed in php-http/message#94