framework icon indicating copy to clipboard operation
framework copied to clipboard

JsonPayloadMiddleware should not try to decode body for GET requests

Open rauanmayemir opened this issue 6 years ago • 5 comments

Currenly, GET requests with Content-Type: application/json will throw exception because JsonPayloadMiddleware tries to decode its empty body.

I think, middleware should fall back to empty arrays in such cases.

Correction: it fails for any kinds of requests with empty body.

rauanmayemir avatar Nov 21 '19 20:11 rauanmayemir

Can you provide me the snapshot (runtime/snaphots) info? Or the details about the specific error.

wolfy-j avatar Nov 22 '19 08:11 wolfy-j

[Spiral\Http\Exception\ClientException]
invalid json payloadin ~/project/vendor/spiral/framework/src/Http/Middleware/JsonPayloadMiddleware.php:35

Exception Trace:
 Spiral\Http\Middleware\JsonPayloadMiddleware->process() at ~/project/vendor/spiral/framework/src/Http/Middleware/JsonPayloadMiddleware.php:35
 Spiral\Http\Middleware\JsonPayloadMiddleware->process() at ~/project/vendor/spiral/http/src/Pipeline.php:82
 Spiral\Http\Pipeline->handle() at ~/project/vendor/spiral/cookies/src/Middleware/CookiesMiddleware.php:59
 Spiral\Cookies\Middleware\CookiesMiddleware->process() at ~/project/vendor/spiral/http/src/Pipeline.php:82
 Spiral\Http\Pipeline->handle() at ~/project/vendor/spiral/http/src/Http.php:100
 Spiral\Http\Http->handle() at ~/project/vendor/spiral/framework/src/Http/RrDispacher.php:80
 Spiral\Http\RrDispacher->serve() at ~/project/vendor/spiral/core/src/ContainerScope.php:50
 Spiral\Core\ContainerScope::runScope() at ~/project/vendor/spiral/core/src/Container.php:272
 Spiral\Core\Container->runScope() at ~/project/vendor/spiral/boot/src/AbstractKernel.php:103
 Spiral\Boot\AbstractKernel->serve() at ~/project/app.php:13

rauanmayemir avatar Nov 22 '19 09:11 rauanmayemir

Technically, the 400 client error is expected when the payload is not valid. I need to think if easing validation is good approach.

wolfy-j avatar Nov 22 '19 15:11 wolfy-j

Yes, I thought of that, too. But from practical standpoint, it would be an odd request to tell API consumers not to include Content-Type: application/json if they're not sending any data.

rauanmayemir avatar Nov 22 '19 15:11 rauanmayemir

OK, I'll check if we can add it to the next release.

wolfy-j avatar Nov 24 '19 11:11 wolfy-j

Fixed https://github.com/spiral/framework/pull/267

butschster avatar Nov 16 '22 15:11 butschster