Replace data on POST?
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?
Is this not possible? Seems like a common use-case to me.
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.
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.
Hi @fishenal,
I would suggest using GET instead, as in REST POST is used to create new data.
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?
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.
Also, it will be great to have a bulk insertion of PUT request. Hope the author will find a solution!
still nothing here? is there a way do replace on POST (or put, or whatever)?
Develop this feature fast