简体   繁体   中英

Header Accept in HTTP

I have a problem with "Accept" header in http. I've writen a http client, and when I set "Accept: image/png" I can still read any file (like txt, html, etc). I think it shouldn't be possible when header "Accept" is set like above.

I tried to check how my Firefox behaves. I wrote "about:config" and I set "network.http.accept.default" as "image/png", and I can surf the net as usually.

Am I misunderstanding meaning of this header? I think that I should only be able to open files *.png.

Accept isn't mandatory; the server can (and often does) either not implement it, or decides to return something else.

If the [Accept] header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.

Source - RFC 7231 5.3.2.Accept

Actually, the former behavior is normal. Let me give you an example.

If the given URL points to a PDF file and the Accept header accepts only docx, then the server will blindly ignore it and send the PDF file because server is not setup to decide between PDF and other documents.

If there are multiple formats available, then server will consider the " Accept " header and try to send the response accordingly, if not, then it will ignore the " Accept " header.

The accept header is poorly implemented by browsers and causes strange errors when used on public sites where crawlers make requests too.

That's why, accept header is ignore most of the time like in the Rail framework .

As you suppose, setting Accept means that you can't accept others medias than these specified, and servers should return a 406 response code. It practice, servers don't implements correctly, and always send a response.

All details are available in RFC 2616

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM