json-server
json-server copied to clipboard
PATCH responds with a 404
hi,
I am trying to patch from react using axios axios.patch(http://localhost:3001/shipments?id=${id}, { 'name': newVal }) but always get back a 404 response.
am I doing something wrong?
To make PATCH requests you want to do something like this (note this is for a native fetch request not axios, but the same logic should apply):
const makePatchRequest = (resource, id, updates) =>
new Request(`http://localhost:3001/${resource}/${id}`, {
method: 'PATCH',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(updates)
})
const req = makePatchRequest('shipments', '1', { name: 'newVal' })
Specifically:
PATCHto/${resource}/${id}not a query for the id. You cannot patch to a query only to an id.- Your updates should be in the body of the request as a stringified object.
- Your request has to have a header with
{ 'Content-Type': 'application/json' } - Make sure your server is using
body-parserto handle JSON bodies