Rocket: Fairing can't change content type

Created on 26 Dec 2017  路  5Comments  路  Source: SergioBenitez/Rocket

Rocket version: 0.3.5
OS: macOS 10.13.2

As stated in the doc:

The Content-Type header is cached after the first call to this function. As a result, subsequent calls will always return the same value.

so there are no way for fairings to change content type. I need this because some clients don't have Content-Type header set in their requests thus rocket always responds with 404, so I want to ensure all requests has Content-Type header set to application/json but I can't do that using rocket fairings.

Since Request already provides set_method and etc. Can we add more set_xxx methods for example set_content_type, set_accpet for cached headers?

deficiency question

Most helpful comment

For now I have extracted rocket_contrib::Json to rocket-lenient-json crate.

I think maybe the Json type should not enforce the Content-Type check by default? Since you can enforce that with format = "application/json" in route function, if the Json type still checks Content-Type header then it's wasting CPU time double checking it.

All 5 comments

I'm curious: why would you want to change the Content-Type of the incoming request?

I think the correct fix is to handle this in Request::replace_header() directly. Then you could simply use something like Request::replace_header(ContentType::JSON) to replace the cached value, which is what you may have expected in the first place.

I did try replace_header in the first place.

Because some client like Python requests for example don't set default content type if you are passing json data using data= kwargs:

import requests
s = '"some json string"'
requests.post('http://localhost:8000/hello', data=s)

while the data itself is valid json. We want to support it because we can't ask every customer to change their code to add the required header.

That's...interesting. So you want to change the Content-Type so that the Json data guard succeeds on some handler? If that's your intention, then I'd recommend that instead of changing the Content-Type on incoming requests, you simply write a new data guard that's essentially identical to the existing Json with the exception that it doesn't do the Content-Type check. Then you can use this new data guard in place of Json and get what you're looking for.

In any case, we should improve replace_header.

For now I have extracted rocket_contrib::Json to rocket-lenient-json crate.

I think maybe the Json type should not enforce the Content-Type check by default? Since you can enforce that with format = "application/json" in route function, if the Json type still checks Content-Type header then it's wasting CPU time double checking it.

:-) i stumbled upon this issue while seearching for the same thing.
I wanted to change the content-type.

The Request beeing sent to the application can't be changed and has Content-Type: application/csp-report (see CSP-Report).

The body is JSON.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcusball picture marcusball  路  3Comments

denysvitali picture denysvitali  路  3Comments

Ronaldho80 picture Ronaldho80  路  3Comments

kitsuneninetails picture kitsuneninetails  路  4Comments

Hokutosei picture Hokutosei  路  4Comments