DEV Community

Olen Daelhousen
Olen Daelhousen

Posted on

Using PATCH on the front end with Fastify

Posting this as a reminder to my future self and with the hope it will save some other folks from banging their heads on their desk for two hours. I like Fastify a lot and was using version 4.x when I had issues with a PATCH endpoint when using the Fetch API on the front end. This was very confusing, since the endpoint worked great when testing with Postman. Here are the things I learned:

  • The value for "method" needs to be all uppercase, "patch" does not work; "PATCH" does
  • The "content-type" header is required, without it, fetch() fails

Here's an example of the code that worked:

const response = await fetch(`${exampleDomain}/api/v1/example`, {
  method: 'PATCH',
  headers: {
    Authorization: `Bearer ${token}`,
    'content-type': 'application/json; charset=utf-8'
    },
  body: JSON.stringify(data)
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)