Codeception: Send file with REST module

Created on 7 Apr 2014  路  16Comments  路  Source: Codeception/Codeception

Hi,

I am trying to send file via sendPOST with REST module, but it doesn`t work. I tried set the third argument as documentation said key => filename. Is it somehow posssible?

REST

Most helpful comment

Remove $I->haveHttpHeader('Content-Type', 'application/json');

All 16 comments

Why is this issue closed?. I've been trying to send files with the REST module without success. I'm getting this error:

Call to undefined method GuzzleHttp\Stream\Stream::addFile() in phar:///usr/local/bin/codecept/src/Codeception/Lib/Connector/Guzzle.php on line 181

Same issue here, any insight on this?

REST modules passes unmodified $files array to BrowserKit\Client:
https://github.com/Codeception/Codeception/blob/031a8927b0edc18418f444e641709034d38a3818/src/Codeception/Module/REST.php#L476

InnerBrowser used by PhpBrowser passed $files array which is in the $_FILES format:
https://github.com/Codeception/Codeception/blob/ed6c3500b97e5f21c89be58a08b8a88ff2ed4963/src/Codeception/Lib/InnerBrowser.php#L482

Guzzle6 and non-Symfony framework connectors expect to get $files in $_FILES format.
Symfony based framework connectors don't do any conversion, but that doesn't mean that they work. Testing is necessary.

BrowserKit Client tests use key => value approach, so it is possible that InnerBrowser is doing it wrong.
https://github.com/symfony/BrowserKit/blob/ca6858ece592b4e31ae9aee7be715a2e7a08842d/Tests/ClientTest.php#L500

We need framework integration tests for file upload, both PhpBrowser and REST.

Guzzle connector still uses non-existing addFile function.

https://github.com/Naktibalda/Codeception/commit/5989899673f078a3bde07c5d931b87a07467e2eb

I created (file upload tests) for REST module and they passed.
addFile method exists.

File upload works with Guzzle 5.3.0 and Guzzle 6.0.2

And all versions of Guzzle 4 actually.

I updated to guzzle 5.3.0 and same issue, and i cannot update for now for Guzzle 6.0.2 beacause, i have php 5.4. I will try to update the version of php and test again with Guzzle 6.0.2

<pre>PHP Fatal Error &#039;yii\base\ErrorException&#039; with message &#039;Call to undefined method GuzzleHttp\Stream\Stream::addFile()&#039;

in /vagrant/m-be-v3/vendor/codeception/codeception/src/Codeception/Lib/Connector/Guzzle.php:194

Now i having the version 6.0.2 and i am using this code for sending images but when i tried to dump the $_FILES is empty.

        $I->haveHttpHeader('Content-Type', 'application/json');
        $I->haveHttpHeader('x-maily-token', $this->_token);
        $I->haveHttpHeader('x-maily-username', $this->_email); 
        $I->sendPOST('/v3/assets', ['file' => 'file'], [codecept_data_dir('test_image.png')]);
        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();

or

$I->sendPOST('/v3/assets', ['sample-field' => 'sample-value'], ['file' => ['name' => 'test_image.jpg', 'type' => 'image/jpeg', 'error' => UPLOAD_ERR_OK, 'size' => filesize(codecept_data_dir('test_image.jpg')), 'tmp_name' => codecept_data_dir('test_image.jpg'), ]]);

I don't have any error but the variable $_FILES is empty.

What i am doing wrong?

Thanks

Remove $I->haveHttpHeader('Content-Type', 'application/json');

I am using this code, and works well. the problem was non-compatibility between php version and guzzlle http

      $I->sendPOST('/v3/assets', null, ['file' => ['name' => 'test_image.jpg', 'type' => 'image/jpeg', 'error' => UPLOAD_ERR_OK, 'size' => filesize(codecept_data_dir('test_image.jpg')), 'tmp_name' => codecept_data_dir('test_image.jpg') ]]);
        $I->seeResponseCodeIs(201);
        $I->seeResponseIsJson();
        $I->seeResponseContains('id');

I hope that this code works for you

I have the same issue: After upgrading to Codeception 2.1 the $_FILES array is empty - no error or exception occurs.

It did not help to change the format of the sendPOST files array as suggested by @diogopms.

I could fix it though by requiring "guzzlehttp/guzzle" : "5.3.*" in composer.json.

Hi @conceptdeluxe 馃憤

I will paste here the code that I amusing to check if a image is correctly sent to the backend server.

$I->wantTo('POST URL');
$I->sendPOST('URL', null, ['file' => ['name' => 'test_image.jpg', 'type' => 'image/jpeg', 'error' => UPLOAD_ERR_OK, 'size' => filesize(codecept_data_dir('test_image.jpg')), 'tmp_name' => codecept_data_dir('test_image.jpg')]]);
$I->seeResponseCodeIs(201);
$I->seeResponseIsJson();
$I->seeResponseContains('id');

I hope that this can help you.

HI. I'm having the same issue as @conceptdeluxe, while I can't downgrade guzzle's version because I need the last one. So, here is my code:

$tester->sendPOST('/tasks/'.$task->id.'/attachments', null, [ 'file' => [ 'name' => 'image.jpg', 'type' => 'image/jpeg', 'error' => UPLOAD_ERR_OK, 'size' => filesize(codecept_data_dir('image.jpg')), 'tmp_name' => codecept_data_dir('image.jpg') ] ]);

And my composer dependencies:
"php": ">=5.6.4", "laravel/framework": "5.4.*", "dingo/api": "dev-master#a852013", "tymon/jwt-auth": "0.5.*", "kuai6/cnd": "*@dev", "watson/validating": "^2.2", "lukasoppermann/http-status": "^2.0", "barryvdh/laravel-ide-helper": "^2.2", "felixkiss/uniquewith-validator": "^2.0", "barryvdh/laravel-cors": "^0.8.1", "codeception/codeception": "2.*", "doctrine/dbal": "^2.5", "league/flysystem-aws-s3-v3": "^1.0", "predis/predis": "^1.1"

This is working fine when I downgrade guzzle to 5.3. But when getting it back to 6.3, the file field comes empty.

Hope someone could help me.

@brunodonadio Have you tried to use a simple key=>value pair way of sending files?

$tester->sendPOST('/tasks/'.$task->id.'/attachments', null, [
    'file' => codecept_data_dir('image.jpg')
]);

Yes @Naktibalda, I have tried that already, but I got the same result as sending the entire file array.

Any suggestion about this?

Works for me.

I used this test code:

$tmpFileName = tempnam('/tmp', 'test_');
file_put_contents($tmpFileName, 'test data');

$I->sendPOST('https://requestb.in/tb4vc4tb', null, ['xxx' => $tmpFileName]);

Result looks correct: https://requestb.in/tb4vc4tb?inspect
Raw body:

Content-Disposition: form-data; name="xxx"; filename="test_mGVkBh"
Content-Length: 9

test data
--181b9b46b57752395123be4e2068dfc91da9820d--
Was this page helpful?
0 / 5 - 0 ratings