neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

On nginx when no `Content-Type` header sent header parser fails

neomerx opened this issue · comments

When client do not sent Content-Type header which is possible for requests without body (e.g. GET or DELETE) header parser fails with message like

#0 /.../vendor/neomerx/json-api/src/Http/Headers/Header.php(105): Neomerx\JsonApi\Http\Headers\MediaType::parse(0, '')
#1 /.../vendor/neomerx/json-api/src/Http/Headers/Header.php(91): Neomerx\JsonApi\Http\Headers\Header::parseMediaType(0, '')
#2 /.../vendor/neomerx/json-api/src/Http/Headers/HeaderParametersParser.php(66): Neomerx\JsonApi\Http\Headers\Header::parse('', 'Content-Type')
#3 /.../vendor/neomerx/limoncello/src/Providers/LaravelServiceProvider.php(449): Neomerx\JsonApi\Http\Headers\HeaderParametersParser->parse(Object(Neomerx\Js$
#4 /.../vendor' in /...vendor/neomerx/json-api/src/Http/Headers/HeaderParametersParser.php:68

The reason is that by default Nginx fastcgi_params config file has

fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

so when no header is sent Nginx sends empty string. The lib treats empty string as an invalid value and throws exception.

The lib should check not only that header exists but it's not empty as well. If not then set it as JSON API media type.

Hello, I got the same problem the other day and I fixed it with the following configuration

fastcgi_param  CONTENT_TYPE       $content_type if_not_empty;
fastcgi_param  CONTENT_LENGTH     $content_length if_not_empty;

I hope it helps somebody else!

@mledoze thanks for the tip 👍