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

Option to create a new Object

Open zimbres opened this issue 1 year ago • 4 comments

Is there an option to create a new object? Thanks!

zimbres avatar Mar 14 '24 15:03 zimbres

Hi! Yes, you can post any data you, with any name (example has abcd) and it will be stored to JSON-file and can be fetched with the name you defined.

# Add new data
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{ "value": "1", "description": "hello", "random": "data" }' http://localhost:57602/api/abcd/

# Fetch created data
curl http://localhost:57602/api/abcd/0

If you have any other questions, just let me know :+1:

ttu avatar Mar 15 '24 15:03 ttu

Hi, in this case, this will create an array inside the object abcd, I what I mean is to create for example:

  "abcd": {
      "value": "1",
      "description": "hello",
      "random": "data",
      "id": 0
    }

zimbres avatar Mar 15 '24 17:03 zimbres

Sorry, misunderstood the question.

Change Api.UpsertOnPut to true from config: https://github.com/ttu/dotnet-fake-json-server/blob/1068151e2912cff5a26fb7f7711f587ecdde5131/FakeServer/appsettings.json#L35

Replace/Add new object with PUT:

# Add new data
curl -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{ "value": "1", "description": "hello", "random": "data" }' http://localhost:57602/api/abcd/

# Fetch created data
curl http://localhost:57602/api/abcd

Apparently there is nothing about objects it in the docs, besides the route list 🤔

PUT      /api/{object}
PATCH    /api/{object}
DELETE   /api/{object}

Thanks for the question. I wouldn't have noticed this without it!

ttu avatar Mar 15 '24 17:03 ttu

Awesome! Thanks a lot!!!

zimbres avatar Mar 16 '24 16:03 zimbres