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

Response sends content-type when no body is sent #1191

Closed
leth opened this issue Sep 19, 2016 · 11 comments
Closed

Response sends content-type when no body is sent #1191

leth opened this issue Sep 19, 2016 · 11 comments
Labels

Comments

@leth
Copy link

leth commented Sep 19, 2016

Long story short

With a recent release the "default" content type became "application/octet-stream".
This appears to be added to responses which do not include a body, so content-type is not relevant.

Expected behaviour

No content-type header present

Actual behaviour

content-type: "application/octet-stream"

Steps to reproduce

Your environment

@asvetlov
Copy link
Member

What behavior it breaks?

@leth
Copy link
Author

leth commented Sep 19, 2016

We have a unit test that checked there was no content type being sent on our API's 404 response, which has no body.

I don't know if it actually breaks anything, it's just not quite in line with the RFCs.
application/octet-stream is only the default content-type IF there is a message body on the request.

@asvetlov
Copy link
Member

Sorry, I couldn't find exact RFC rule for this.
Would you mind pointing to the reference?

@leth
Copy link
Author

leth commented Sep 19, 2016

The default references RFC 2616. I've down broken the paragraph I think it's referencing below:

Any HTTP/1.1 message containing an entity-body SHOULD include Content-Type header field defining the media type of that body.

If there is an entity-body sending a Content-Type header is advised, but optional.

If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource.

If missing, clients are permitted to guess.

If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

If clients can't guess, fall back to "application/octet-stream"

I don't think this suggests that an HTTP server should by default send a header with a default value.
Perhaps what's happened here, is that client and server code have become shared, and some odd behavior has crept in!

@asvetlov
Copy link
Member

Well, the rule for not sending Content-Type is very complicated actually.

  • Present if Content-Length is present and not 0.
  • Present if chunked encoding is used (Transfer-Encoding: chunked).
  • Present if implicit BODY are sent and user has improperly configured headers. It's still possible when StreamResponse is used.
  • Present if no BODY is sent but request's type was HEAD.
  • Absent if no BODY is sent and request's type is not HEAD.

The problem is: on headers sending stage it's impossible to figure out will user send data later or not?

The change was introduced by #1124 (fix for #944) and 3d53ba8

I don't think that server's behavior is incorrect now. It shouldn't break any existing client I believe.

@leth
Copy link
Author

leth commented Sep 23, 2016

Hmm. Okay so is there a easy way to tell aiohttp to not send a content-type?

The reason we do this is our 404 response has no body but used to send a content-type heaser; Firefox would try to parse the empty body and would complain that the json was malformed.

@asvetlov
Copy link
Member

del response.headers['Content-Type'] should help.

@asvetlov
Copy link
Member

BTW default Content-Type is application/octet-stream.
If Firefox complains about malformed json maybe your code sets the type to application/json somewhere?

@leth
Copy link
Author

leth commented Sep 23, 2016

It used to, when I originally fixed that I added the test to assert there was no header, which led to this issue when we upgraded!

Anyway, if it's simple to work around, and won't break anything then I'm happy to close this.
Thanks!

On 23 September 2016 14:21:56 BST, Andrew Svetlov notifications@github.com wrote:

BTW default Content-Type is application/octet-stream.
If Firefox complains about malformed json maybe your code sets the type
to application/json somewhere?

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#1191 (comment)

Sent from my phone. Please excuse my brevity.

@asvetlov
Copy link
Member

Sure!

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants