dingo / api

A RESTful API package for the Laravel and Lumen frameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Header values must be RFC 7230 compatible strings

juansebasleal opened this issue · comments

In a Laravel Controller, I am returning $this->response->noContent(), which returns a Dingo Response.
Then, I'm running a validator on the response and I'm getting the following error:

Header values must be RFC 7230 compatible strings

I see what is causing it is:

$defaultContentType = $this->headers->get('Content-Type');
$this->headers->set('Content-Type', $formatter->getContentType());

in the Dingo\Api\Http\Response::morph() method

$this->headers->set('Content-Type', $formatter->getContentType());

I think there should be a validation: if ($formatter->getContentType()) { // Add content type }

Our current Dingo version is 2.0.1

Thanks.

commented

Hi @juansebasleal - can you explain why it is actually not valid and provide an example ?

Hi @specialtactics
Well, if the response has not actual body (HTTP 204 response), it should not have Content-Type because there's no content.

The example would be easy to setup: In any controller just respond with DingoResponse->noContent().
Also, here you can see an example of how we get Content-Type: (null)
image

What I think is that it happens because in api/src/Http/Response.php, method morph(), $formatter->getContentType() is null.

Additionally, this is more evident when we use a validator, in this case https://github.com/Nyholm/psr7, and we see that in fact it fails because of what was said above.

My environment:
Laravel: 5.7.15
Dingo: 2.0.1
Validator: 1.3.0

Hopefully this helps clarify the issue.

commented

Hey @juansebasleal thanks, that was quite detailed.

I believe I've fixed the issue now, you can try the latest master if you like to confirm, it will be tagged before too long but there are also other changes there for testing.