There are many artists with career spanning several decades and many of them trying to fit into the current trends as well. But David Bowie was the only one who could actually be called a chameleon. He didn’t have to try to fit into new musical styles; he anticipated them, shaped them, and influenced their most relevant artists. And then, he could humbly create fantastic covers of one of their songs.

Response headers can contain valuable information and may help to keep your API responses more simple by separating the actual response data from accesory metadata.

For instance, when querying the WordPress JSON API for a list of posts, the response body includes just the posts data but the pagination info it’s sent as headers:

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Authorization
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
X-WP-Total: 7
X-WP-TotalPages: 2

Whether you’re using jQuery or just plain Javascript, it’s quite simple to get response headers.

Using jQuery

I’ll keep using the same case of getting a list of posts from the WordPress API.

The jQuery code for the AJAX request would looks something like this:

var posts = $.ajax({
  type: 'GET'
  url: '/wp-json/wp/v2/posts/',
  data: {
    'per_page': 10
  }
});

In jQuery, an AJAX request returns a promise, so after that we could do someting like:

posts.done( function( data, textStatus, jqXHR ){
  var receiver = $('#posts-receiver');
  for ( var i in data ){
    receiver.append( data.title.rendered +':'+ data.link );
  }
  var current_page = parseInt( receiver.data('paged'), 10 ),
      total_pages  = parseInt( jqXHR.getResponseHeader('X-WP-TotalPages'), 10 );
  if ( curent_page == total_pages ) {
    $('button#load-more-posts').attr('disabled', 'disabled');
  }
} );

The jqXHR object it’s a superset of the native XMLHttpRequest object. It exposes a jqXHR.getResponseHeader() method for getting a single response header (which it’s always returned as a string) and jqXHR.getAllResponseHeaders() for getting all of them as a single string.

Using plain javascript

The same getResponseHeader method is available in plain javascript as part of the XMLHttpRequest object; the only thing that changes it’s the way you create and initiate the request:

var request = new XMLHttpRequest;
request.open('GET', '/wp-json/wp/v2/posts', true);
request.onload = function(){
  // do some stuff
  var total_pages = parseInt( request.getResponseHeader('X-WP-TotalPages), 10 );
};
request.send();

Unified search with Elasticsearch and WordPress

During the last months of 2012, and as a part of AyerViernes, we worked on one of those projects that is as challenging as delightful to take part in, developing a unified search system for a network of over 200 WordPress sites (both single-install and multisite).

We developed a real-time sync plugin integrating the WordPress sites with an Elasticsearch instance with different content types (mappings) that give us plenty of room to index and search in hundreds of thousands documents generated by the university staff.

You can read the complete post (in spanish) on Medium: Desarrollo de sistema de búsqueda transversal: +200 sitios a un clic de distancia

How Mickey Mouse Evades the Public Domain

How Mickey Mouse Evades the Public Domain tells the story of how every time the cartoon it’s about to enter the public domain, corporate lobbying it’s able to bend existing legislation to protect private interests.

A Practical Guide to SVGs on the web

A Practical Guide to SVGs on the web — might as well be called “everything you need to know for a modern use of SVGs on your website”, from preparing, implementing to manipulating and animating…

UX Myths

UX Myths collects the most frequent user experience misconceptions (such as “people don’t scroll”, “mobile users are distracted” or “accesible sites are ugly”) and explains why they don’t hold true, with research findings and articles.

Patronizing Passwords

Password Security Guide it’s an alternative interaction pattern for password meters that not only helps your users choose a better password but also explains and educates.