DEV Community

Olen Daelhousen
Olen Daelhousen

Posted on

1

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)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️