-
Notifications
You must be signed in to change notification settings - Fork 602
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
@std/http/route
is doesn't automatically route HEAD
requests
#5993
Comments
@std/http/route
is not friendly for HEAD request method@std/http/route
is doesn't automatically route HEAD
requests
|
I think HEAD handler and GET handler shouldn't be exactly the same in general as HEAD response should have empty body.
What is the response body for HEAD request in oak? Does it become empty, or is it the same as GET response? |
As https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD said: any representation headers that describe the erroneous body are assumed to describe the response body that a GET request would have received. If you manually change the response body to empty, its Content-Length will also change. Which might not be a correct behavior for some cases. Whether a response body and Content-Length need to be returned is decided by the endpoint provider themselves, rather than us thinking that a HEAD request is unreasonable just because we see a response body.
It's nothing to do with Oak. You don't need to care about the response body. Tools like cURL, Postman, and RapidAPI will handle this themselves. |
RFC 9110 says: The HEAD method is identical to GET except that the server MUST NOT send content in the response. ref: https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.2 What this suggestion tries to promote is providing the same handler for GET and HEAD, but that violates the above rule.
This rule looks like for the clients to work around the non-compliant servers. I don't think the servers should intentionally depend on the workaround. |
Thank you for finding this out. This is fine for no HEAD on default behavior. My main question was whether we could provide developers with a more convenient way to handle HEAD requests. Currently, we have to define the route twice for these two methods. It would be good if we could have We could change the // route.ts
match &&
(Array.isArray(route.method)
? route.method.includes(request.method)
: (route.method ?? 'GET') === request.method) type Route = {
pattern: URLPattern;
method?: string | string[]; // default to `'GET'`
handler: Handler
};
const exampleHandler: Handler = (requset) => {
const body = runSomeLogic();
return new Response(request.method === 'HEAD' ? null : body);
};
const route: Route = [
{
method: ['HEAD', 'GET'],
handler: exmapleHandler,
},
];
What do you think? |
It's difficult to handle the HEAD request method on
route()
. For example:The route configuration above only supports
GET
request method. You have to add three extra routes with{ method: "HEAD", ... }
.What I expect is that all configured methods allow HEAD requests by default. And add an extra option to let users choose strict mode that excludes HEAD.
Or we can change it
method
option tomethods
, so that users can define multiple handlers for one handler:or use add option
allowHead
:Oak's
route.get()
handles theHEAD
method as expected. The same is true for other frameworks, and we may need to consider polishing this part of the experience for developers.The text was updated successfully, but these errors were encountered: