directus / v8-archive

Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
https://docs.directus.io/api/reference.html
507 stars 203 forks source link

Fileupload failed #2342

Open dgram opened 4 years ago

dgram commented 4 years ago

🐞 Bug report

Description

I've got the same error as described in directus/sdk-js#78 for my node FormData Upload. So i try to upload files via directus-sdk-js on my node server and got the message Missing boundary in multipart/form-data POST data

🔬 Minimal Reproduction

import * as FormData from 'form-data';
import SDK from '@directus/sdk-js';

const sdk = new SDK({...});
let data = new FormData();
// image.content = Buffer
data.append('file', image.content, image.filename);

try {               
    let upload = await sdk.uploadFiles(data);
} catch (e) {
    console.log(e)
}

🔥 Exception or Error

PHP Server Warning


 Missing boundary in multipart/form-data POST data in Unknown

Directus Exception


[2020-01-27 21:28:45] api[Marketing].ERROR: Directus\Validator\Exception\InvalidRequestException: data: This value should not be blank. filename_disk: This value should not be blank. filename_download: This value should not be blank. in D:\directus\src\core\Directus\Services\AbstractService.php:186
Stack trace:
#0 D:\directus\src\core\Directus\Services\AbstractService.php(120): Directus\Services\AbstractService->throwErrorIfAny
directus/sdk-js#1 D:\directus\src\core\Directus\Services\FilesServices.php(48): Directus\Services\AbstractService->validate
directus/sdk-js#2 D:\directus\src\endpoints\Files.php(75): Directus\Services\FilesServices->create
directus/sdk-js#3 [internal function]: Directus\Api\Routes\Files->create
directus/sdk-js#4 D:\directus\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(40): call_user_func
directus/sdk-js#5 D:\directus\vendor\slim\slim\Slim\Route.php(281): Slim\Handlers\Strategies\RequestResponse->__invoke
directus/sdk-js#6 D:\directus\src\core\Directus\Application\Http\Middleware\AbstractRateLimitMiddleware.php(34): Slim\Route->__invoke
directus/sdk-js#7 [internal function]: Directus\Application\Http\Middleware\AbstractRateLimitMiddleware->__invoke
directus/sdk-js#8 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#9 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#10 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#11 D:\directus\src\core\Directus\Application\Http\Middleware\AuthenticationMiddleware.php(124): Slim\Route->Slim\{closure}
directus/sdk-js#12 [internal function]: Directus\Application\Http\Middleware\AuthenticationMiddleware->__invoke
directus/sdk-js#13 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#14 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#15 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#16 D:\directus\src\core\Directus\Application\Http\Middleware\TableGatewayMiddleware.php(25): Slim\Route->Slim\{closure}
directus/sdk-js#17 [internal function]: Directus\Application\Http\Middleware\TableGatewayMiddleware->__invoke
directus/sdk-js#18 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#19 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#20 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#21 D:\directus\src\core\Directus\Application\Http\Middleware\DatabaseMigrationMiddleware.php(15): Slim\Route->Slim\{closure}
directus/sdk-js#22 [internal function]: Directus\Application\Http\Middleware\DatabaseMigrationMiddleware->__invoke
directus/sdk-js#23 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#24 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#25 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#26 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->Slim\{closure}
directus/sdk-js#27 D:\directus\vendor\slim\slim\Slim\Route.php(268): Slim\Route->callMiddlewareStack
directus/sdk-js#28 D:\directus\vendor\slim\slim\Slim\App.php(503): Slim\Route->run
directus/sdk-js#29 D:\directus\src\core\Directus\Application\Http\Middleware\AbstractRateLimitMiddleware.php(34): Slim\App->__invoke
directus/sdk-js#30 [internal function]: Directus\Application\Http\Middleware\AbstractRateLimitMiddleware->__invoke
directus/sdk-js#31 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#32 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#33 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#34 D:\directus\vendor\directus\proxy-detection\src\ProxyDetectionMiddleware.php(30): Slim\App->Slim\{closure}
directus/sdk-js#35 D:\directus\src\core\Directus\Application\Http\Middleware\ProxyMiddleware.php(18): RKA\Middleware\ProxyDetectionMiddleware->__invoke
directus/sdk-js#36 [internal function]: Directus\Application\Http\Middleware\ProxyMiddleware->__invoke
directus/sdk-js#37 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#38 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#39 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#40 D:\directus\vendor\akrabat\ip-address-middleware\src\IpAddress.php(113): Slim\App->Slim\{closure}
directus/sdk-js#41 [internal function]: RKA\Middleware\IpAddress->__invoke
directus/sdk-js#42 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#43 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#44 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#45 D:\directus\src\core\Directus\Application\Http\Middleware\CorsMiddleware.php(71): Slim\App->Slim\{closure}
directus/sdk-js#46 [internal function]: Directus\Application\Http\Middleware\CorsMiddleware->__invoke
directus/sdk-js#47 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#48 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#49 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#50 D:\directus\src\core\Directus\Application\Http\Middleware\ResponseCacheMiddleware.php(62): Slim\App->Slim\{closure}
directus/sdk-js#51 [internal function]: Directus\Application\Http\Middleware\ResponseCacheMiddleware->__invoke
directus/sdk-js#52 D:\directus\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
directus/sdk-js#53 [internal function]: Slim\DeferredCallable->__invoke
directus/sdk-js#54 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
directus/sdk-js#55 D:\directus\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}
directus/sdk-js#56 D:\directus\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack
directus/sdk-js#57 D:\directus\vendor\slim\slim\Slim\App.php(297): Slim\App->process
directus/sdk-js#58 D:\directus\src\core\Directus\Application\Application.php(161): Slim\App->run
directus/sdk-js#59 D:\directus\public\index.php(4): Directus\Application\Application->run [] []

🌍 Your Environment

SDK Version: v6.0.0 Directus Version: v8.4.0 PHP-Version: 7.3.9

rijkvanzanten commented 4 years ago

I think this is caused by the fact that the headers don't contain the correct boundary in this case. As per https://www.npmjs.com/package/form-data#axios, we should add a way to override the headers for this specific request so you can use const formHeaders = form.getHeaders(); to pass it the header with the correct boundary