Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active September 18, 2022 20:39
Show Gist options
  • Save ldong/c52f491a88935a8c24337c1326586f99 to your computer and use it in GitHub Desktop.
Save ldong/c52f491a88935a8c24337c1326586f99 to your computer and use it in GitHub Desktop.
Ember Data REST response

Ember Data REST response

createRecord

URL: /users/

Method: POST

Request Data:

{
  "user": {
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe"
  }
}

Response Data:

{
  "user": {
    "id": "1",
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe"
  }
}

findRecord

URL: /users/123

Method: GET

Request Data: NOTHING

Response Data:

{
  "user": {
    "id": "123",
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe"
  }
}

findAll

URL: /users/

Method: GET

Request Data: NOTHING

Response Data:

{
  "users": [
    {
      "user": {
        "id": "1",
        "email": "[email protected]",
        "first_name": "John",
        "last_name": "Doe"
      }
    },
    {
      "user": {
        "id": "2",
        "email": "[email protected]",
        "first_name": "Jane",
        "last_name": "Doe"
      }
    }
  ]
}

query

Example: store.query('user', { sort: 'first_name', gender: 'male' });

URL: /users?gender=male&sort=first_name

Method: GET

Request Data: NOTHING

Response Data:

{
  "users": [{
    "id": "1",
    "email": "[email protected]",
    "first_name": "Adam",
    "last_name": "Doe",
    "gender": "male"
  }, {
    "id": "20",
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe",
    "gender": "male"
  }]
}

queryRecord

Example: store.queryRecord('user', { first_name: 'John' })

URL: /users?first_name=John

Method: GET

Request Data: NOTHING

Response Data:

{
  "user": {
    "id": "1",
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe"
  }
}

updateRecord

Example:

store.findRecord('user', 1).then(user => {
  user.set('first_name', "Jane");
  return user.save();
});

URL: /users/1

Method: PUT

Request Data:

{
  "user": {
    "id": "1",
    "email": "[email protected]",
    "first_name": "Jane",
    "last_name": "Doe"
  }
}

Response Data:

{
  "user": {
    "id": "1",
    "email": "[email protected]",
    "first_name": "Jane",
    "last_name": "Doe"
  }
}

deleteRecord

Examples

store.findRecord('user', 1).then(function(user) {
  user.deleteRecord();
  user.get('isDeleted'); // => true
  user.save(); // => DELETE to /users/1
});

// OR
store.findRecord('user', 2).then(function(user) {
  user.destroyRecord(); // => DELETE to /users/2
});

URL: /users/2

Method: DELETE

Request Data: NOTHING

Response Data: NOTHING

Reference

  1. https://stackoverflow.com/questions/14922623/what-is-the-complete-list-of-expected-json-responses-for-ds-restadapter#
  2. Ember Data 2.18 https://github.com/emberjs/data/blob/master/tests/integration/adapter/rest-adapter-test.js 3 Updating documentation with the default expected API responses. · Issue #3257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment