Mdd
Mdd

Reputation: 4400

Edit RESTAdapter url for API end point

I have an api endpoint that requires a / at the end of it, but Ember does not add the /. Is there a way to edit the URL that the RESTAdapter creates so that it adds this slash?

Currently the URL ember sends is http://www.myapi.com/v1/roles

I need the URL to look like this: http://www.myapi.com/v1/roles/

Here is my current ApplicationAdapter:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  ajaxError: function() {
    console.log('error');
  },

  host: 'http://www.myapi.com',

  namespace: 'v1'

});

Here is my router:

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(params) {
    return this.store.find('role');
  }

});

Upvotes: 1

Views: 571

Answers (1)

Paul
Paul

Reputation: 18597

You'll want to override the buildURL function on your ApplicationAdapter to append the trailing slash. You can just call the default buildURL that DS.RESTAdapter provides and then append the slash.

Here's what the code will look like:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  ajaxError: function() {
    console.log('error');
  },

  host: 'http://www.myapi.com',

  namespace: 'v1',

  buildURL: function(type, id, record) {
    //call the default buildURL and then append a slash
    return this._super(type, id, record) + '/';
  }

});

Here's the documentation for buildURL.

Upvotes: 5

Related Questions