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

PUT request deletes some attributes.

Open XiaoyuSun opened this issue 10 years ago • 2 comments

for example: In the json.db

{
  users: 
  [
      {
       "id": 1,
       "name": "Leanne Graham",
       "username": "Bret",
       "email": "[email protected]",
       "address": {
         "street": "Kulas Light",
         "suite": "Apt. 556",
         "city": "Gwenborough",
         "zipcode": "92998-3874"
      }
  ]
}

Then sending a PUT request to update {"address": {"street": "a new street"}}, the data changed to

{
  "users": [
    {
      "address": {
        "street": "json-server-street-23"
      },
      "id": 1
    }
  ]
}

All other attributes, such as: username, city are deleted. Is it possible to keep all other attributes but just update the street only?

I am using json-api resource on the backend and trying to using json-server to mock all json-api resource functionality.

Thanks!

XiaoyuSun avatar Jun 13 '16 23:06 XiaoyuSun

What you're looking for here is the PATCH method: https://tools.ietf.org/html/rfc5789. Generally speaking, PUT is meant to entire replace a resource with the new attributes, and PATCH should be used for partial application. json-server does provide the PATCH method, and it would seem to suit your needs here.

drewen avatar Jul 26 '16 21:07 drewen

@drewen This works! I was about to go shoot some ducks! Thanks mate!

kamrul91111 avatar Aug 17 '21 00:08 kamrul91111