Skip to content

DataAPI SDK english MT.DataAPI Endpoint methods overview

Taku AMANO edited this page Jul 5, 2013 · 1 revision

Overview - Accessing API endpoint

Method paramaters

The :site_id is called a method parameter for the /:site_id/entries endpoint.

You can pass a method parameter by various format.

By the number

var siteId = 1;
api.listEntries(siteId, function(response) {
  // Do stuff
});

By the object

var site = {
  id: 1
};
api.listEntries(site, function(response) {
  // Do stuff
});

By the function

var siteId = function() { return 1 };
api.listEntries(siteId, function(response) {
  // Do stuff
});

By the function through the object

var site = {
  id: function() { return 1 }
};
api.listEntries(site, function(response) {
  // Do stuff
});

Query parameters and request body

You can pass query parameters and request body by various format.

By the string

var query = 'search=QUERY';
api.listEntries(site, query, function(response) {
  // Do stuff
});

By the object

var query = {
  search: 'QUERY'
};
api.listEntries(site, query, function(response) {
  // Do stuff
});

By the HTMLFormElement object

<form id="search-form">
  <input type="text" name="search" />
</form>
<script type="text/javascript">
var query = document.getElementById('search-form');
api.listEntries(site, query, function(response) {
  // Do stuff
});
</script>

By the FormData object

// This code requires that your browser had implemented FormData.
var query = new FormData();
query.append('search', 'query');
api.listEntries(site, query, function(response) {
  // Do stuff
});

Resource in request body

You can pass a resource to create or update by various format.

By the object

// The resource data does not require strictness.
// Requires only values you want to create or to update.
// Allows including Unnecessary value.
var data = {
  title: 'TITLE',
  body: 'BODY'
};
api.updateEntry(site, entryId, data, function(response) {
  // Do stuff
});

Or, you can simplly pass the data that had been retrieved from API.

api.getEntry(site, entryId, function(entry) {
  entry.title = 'UPDATED TITLE';
  api.updateEntry(site, entryId, entry, function(response) {
    // Do stuff
  });
});

By the HTMLFormElement

<form id="entry-form">
  <input type="text" name="title" />
  <textarea name="body" />
</form>
<script type="text/javascript">
var data = document.getElementById('entry-form');
api.createEntry(site, data, function(response) {
  // Do stuff
});
</script>

Send a file

You can pass a file by various format.

However, you must not forget that in every way, you can not open file selection using JavaScript in IE. The following code makes a security error in IE.

<form>
  <input type="file" name="file" id="file" />
</form>
<a href="#" id="select-file">Select file</a>
<script type="text/javascript">
document.getElementById("select-file").onclick = function() {
  getElementById("file").click();
  return false;
};

var data = {
  file: getElementById("file").files[0],
};
api.uploadAsset(siteId, data, function(response) {
  if (response.error) {
    // Handle error
    return;
  }

  // Do stuff
});
</script>

You should rewrite these cases as follows.

<form>
  <input type="file" name="file" id="file" />
</form>
<label for="file">Select file</label>
<script type="text/javascript">
var data = {
  file: getElementById("file").files[0],
};
api.uploadAsset(siteId, data, function(response) {
  if (response.error) {
    // Handle error
    return;
  }

  // Do stuff
});
</script>

By the File object

var data = {
  file: getElementById('file-input').files[0]
};
api.uploadAsset(siteId, data, function(response) {
  // Do stuff
});

By the HTMLInputElement

var data = {
  file: getElementById('file-input')
};
api.uploadAsset(siteId, data, function(response) {
  // Do stuff
});

By the HTMLFormElement

<form id="asset-form">
  <input type="file" name="file" />
</form>
<script type="text/javascript">
var data = document.getElementById('asset-form');
api.createEntry(site, data, function(response) {
  // Do stuff
});
</script>

Response object

When succeeded

api.createEntry(site, data, function(response) {
  console.log(response); // Response data
});

When error

api.createEntry(site, data, function(response) {
  if (response.error) {
    console.log(response.error.code);    // The HTTP response code
    console.log(response.error.message); // The error message
    console.log(response.error.data);    // If current error has some optional data
  }
});
Clone this wiki locally