Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define Static File Content Type #1266

Closed
MyNameIsCosmo opened this issue Jul 19, 2018 · 0 comments · Fixed by #1267
Closed

Define Static File Content Type #1266

MyNameIsCosmo opened this issue Jul 19, 2018 · 0 comments · Fixed by #1267

Comments

@MyNameIsCosmo
Copy link
Contributor

While trying to serve a utf-8 encoded HTML document via Sanic.static, I noticed that content_type cannot be set using Sanic.static.

Sanic header content_type defaults to text/plain
if the type cannot be guessed by mime_types.guess_type, which seems to only analyze the filename.

Currently, the following is not possible (same goes for blueprints):

app.static('/', get_data('index.html'), content_type='text/html; charset=utf-8')

Prior to this PR, you need to do the following to serve a document with a custom content-type:

@app.route('/')
async def handle_index(request):
    return await response.file(
        'index.html',
        headers={'Content-Type': 'text/html; charset=utf-8'}
    )

I'll be submitting a PR shortly with a proposed fix.

MyNameIsCosmo added a commit to MyNameIsCosmo/sanic that referenced this issue Jul 19, 2018
r0fls pushed a commit that referenced this issue Jul 21, 2018
* Add content_type flag to Sanic.static

Fixes #1266

* Fix flake8 error in travis

Add line to document `content_type` arg

* Fix content_type for file streams

Update tests

herp derp

* Remove content_type as an arg to HTTPResponse

`response.HTTPResponse` will default to `headers['Content-Type']` instead of `content_type`
#1267 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant