npm - json-server can we use other key instead of id for post and put request -


i have fake api testing in frontend side.

i have seen id required put or post data in json-server package, question can use different key instead of id ex.

{   id: 1, ---> want change custom id   name: 'test' }  

let's see cli options of json-server package: $ json-server -h

... --id, -i   set database id property (e.g. _id)   [default: "id"] ... 

let's try start json-server new id called 'customid' (for example): json-server --id customid testdb.json

structure of testdb.json file: $ cat testdb.json

{   "messages": [     {       "customid": 1,       "description": "somedescription",       "body": "sometext"     }   ] } 

make simple post request via $.ajax function (or via fiddler/postman/etc.). content-type of request should set application/json - explanation may found on project's github page:

a post, put or patch request should include content-type: application/json header use json in request body. otherwise result in 200 ok without changes being made data.

so... make request browser:

$.ajax({   type: "post",   url: 'http://127.0.0.1:3000/messages/',   data: {body: 'body', description: 'description'},   success: resp => console.log(resp),   datatype: 'json' }); 

go testdb , see results. new chunk added. id automatically added desired name specified in --id key of console cmd.

{ "body": "body", "description": "description", "customid": 12 }

voila!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -