json-server icon indicating copy to clipboard operation
json-server copied to clipboard

PATCH responds with a 404

Open aiyk opened this issue 7 years ago • 1 comments

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?

aiyk avatar May 13 '19 11:05 aiyk

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:

  • PATCH to /${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-parser to handle JSON bodies

samuelcolburn avatar Jul 29 '19 19:07 samuelcolburn