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

404 File Not Found uWebSockets/20 Server on PATCH #1061

Closed
josh-i386g opened this issue May 17, 2024 · 3 comments
Closed

404 File Not Found uWebSockets/20 Server on PATCH #1061

josh-i386g opened this issue May 17, 2024 · 3 comments

Comments

@josh-i386g
Copy link

Hi everyone good day.

Has anyone encountered unbound handlers on PATCH method that redirects to 404 recently?

I'm generating CRUD endpoints for my tables. The following methods work:

  • GET for reading one record
  • GET for reading many records
  • POST for creating a record
  • PUT for updating a record
  • DELETE for deleting a record

The problem is I also want to support PATCH handler because my PUT handler supports partial column updates. But when I try to bind it to PATCH handler, it doesn't get caught, and ends up to the default 404 handler.

I am using "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.43.0"

image

image

image

image

image

When I remove the codes that use PATCH, all tests work:

image

For now I"ll be using PUT, but decided to create this issue in case someone else encounters similar issue, thank you.

@uNetworkingAB
Copy link
Contributor

I can serve a PATCH request here, can you provide a small snippet that fails?

@uNetworkingAB
Copy link
Contributor

alexhultman@Windows11:~/uWebSockets.js/uWebSockets$ curl --request PATCH --verbose http://localhost:3000
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> PATCH / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 22 May 2024 02:11:06 GMT
< uWebSockets: 20
< Content-Length: 12
< 
* Connection #0 to host localhost left intact
Hello world!alexhultman@Windows11:~/uWebSockets.js/uWebSockets$ 

@josh-i386g
Copy link
Author

Hi, turns out the problem is in my test code, not in uWebSockets.js.

The cause is Node.js internal fetch (undici) doesn't normalize "patch" into "PATCH" (which surprisingly follows the spec).

Thread here: nodejs/undici#2294

The fix in my code:

    const url = new URL("http://localhost:8080/");
    url.pathname = `/api/${resource}/${item.id}`;
-   const method = "patch";
+   const method = "PATCH";
    const headers = new Headers();
    if (typeof token === "string") {
      headers.set("authorization", `Bearer ${token}`);
    }
    headers.set("content-type", "application/json; charset=utf-8");
    const body = JSON.stringify(item);
    const response = await fetch(url, { method, headers, body });

Closing this, thank you!

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

No branches or pull requests

2 participants