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

Replace data on POST?

Open hamishtaplin opened this issue 10 years ago • 9 comments

I want to be able to post a whole new set of data to my endpoint /blocks/. So, if I start with an empty JSON object like so:

{
  "blocks": [ ]
}

and then post an array of objects to it. Currently, it will push an array into the blocks array like so:

{
  "blocks": [
     [ {
      "title": "Dropdown Question",
      "type": "dropdown-question",
      "id": 0
    },
    {
      "title": "Multiple Choice Single Answer",
      "type": "multiple-choice-single-answer",
      "id": 1
    } ]
  ]
}

Further posts will keep appending more arrays to blocks.

Instead, I want to replace the value of blocks. So, on first post it will create the top-level array and further calls will replace that array?

hamishtaplin avatar Dec 22 '15 10:12 hamishtaplin

Is this not possible? Seems like a common use-case to me.

hamishtaplin avatar Jan 07 '16 09:01 hamishtaplin

I think using POST to replace collection of resources is not a common way to do it. Correct me if I'm wrong. I'd rather delete all posts with DELETE /posts and then POST new collection. Unfortunately, it seems that deleting entire collection is currently not suported by json-server.

slawekkopacz avatar May 16 '16 19:05 slawekkopacz

same question.

I want use this json-server with vuejs, I found when I make a post request, It just replace the data from 'bd.json', In my situation, I just want use POST request to get some data via params, seems like POST means here is to post some data and replace old.

fishenal avatar Oct 26 '16 03:10 fishenal

Hi @fishenal,

I would suggest using GET instead, as in REST POST is used to create new data.

typicode avatar Oct 26 '16 08:10 typicode

Noticing the same thing where if I POST an array of objects it creates a sub array. Is there way to POST an array of objects normally?

housseindjirdeh avatar Dec 22 '16 00:12 housseindjirdeh

I'm looking at this too although I'm wanting a Bulk Insert option. One why I would consider handling this is having the server detect if req.body is an object or an array. In the latter case you could iterate over the array and do a transactional process to insert each object.

Since this is likely just a mock server and it's using lowdb it looks like we could just do this with something like what the docs there show:

import low from 'lowdb/lib/fp'
const db = low()

// Get or set posts
const posts = db('posts', [])

But how do we get a handle to db from json-server? Also this feels like a nice add for the default middleware rather than needing do this manually.

This might address others issues as well. While not overwriting stuff you could issue a bulk delete and then a bulk insert. Still one extra step, but through explicit intent.

r4lphio avatar Feb 18 '17 00:02 r4lphio

Also, it will be great to have a bulk insertion of PUT request. Hope the author will find a solution!

tousisat avatar May 31 '18 02:05 tousisat

still nothing here? is there a way do replace on POST (or put, or whatever)?

GsHeri avatar Feb 26 '19 12:02 GsHeri

Develop this feature fast

shinoshu avatar Jun 19 '19 04:06 shinoshu