Skip to content

Simple API

StefansArya edited this page May 4, 2020 · 2 revisions

This feature give you simple interface for using sf.request for accessing API with your server. Scarlets framework are support this feature.

Create new instance

// The first parameter are required to be filled with your API URL.
// You don't need the last slash "/"
var myAPI = new sf.API('http://example.com/api');

Adding access token

After you added the access token, X-Authorization: Bearer {token here} header will be added on every HTTP request.

myAPI.accessToken = "{token here}";

Note

This feature will wrap your HTTP method inside _method on the request data and send HTTP request with POST method. To turn off this behavior you set myAPI.mask to false.

Basic usage

data and beforeSend parameter are optional

myAPI.request(method, url, data, beforeSend);

// Example URL-> http://example.com/api/add-user
myAPI.request('POST', '/add-user', {name:"Alex"})
  .done(function(data, statusCode){
    // The response data will automatically being parsed from JSON to Object
    console.log("Got user ID:", data.id);
  });

myAPI.request will return XMLHttpRequest just like sf.request.

Shortcut

data parameter are optional

myAPI.get(url, data);
myAPI.post(url, data);
myAPI.delete(url, data);
myAPI.put(url, data);

Clone this wiki locally