Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.1 The Index Route

In this lesson, we'll explore the API by sending a GET request to the index route. I'll also show you an easy way to check if the WP REST API is actually installed and enabled on a website. You'll learn how the index route documents all the routes that the WP REST API provides, their endpoints, and their respective supported arguments.

4.1 The Index Route

Hey, folks, and welcome back to the course introducing the WP REST API. I'm instructor, Bilal Shahed. Now that we have set up a working environment, we are now finally ready to work with the WP REST API. We will begin by checking the index route of the API by sending a GET request. But before that, we might need to check if the WP REST API is actually installed and enabled on the site that we are working with. This is done by sending a HEAD request to the URL of the web site. Sending a HEAD request to a URL only returns the response headers which can then be accessed through the header step in Postman. In this collection of headers, this link header contains the value of the index route of the WP REST API. Alternatively, we can achieve the same result by sending a GET request to the same URL. But, sending a GET request returns response headers as well as the response body. So as you can conclude, sending a HEAD request is more efficient method. Now that we have retrieved the index route of the WP REST API, we can send the GET request to retrieve some very fundamental information about the WP REST API plugin. This information is returned in JSON format and it includes seven properties which are name, description, URL, namespaces, authentication, routes and finally _links. The first four properties are pretty self explanatory, and they contain values for the name, description and the URLs of the Wordpress site we are working with. The next namespaces property is an area of all the namespaces that are registered by WP REST API or a third party plugin. Currently there are two elements in this area that are wp/v2 and oembed/1.0. These two namespaces are registered by WP REST API plugin itself. Namespaces provide a way for plugins to avoid overwriting that might occur when each plugin is providing its own functionality. Hence, each plugin can provide its own functionality without the fear of being overwritten by what is provided by some other plugin. The authentication area lists all the authentication methods that are supported by the API. Currently, we have the basic authentication plugin installed, but it will not show up here, since the basic authentication method is not secure enough to be used in production environments. The most important property here, is this routes property that lists all the supported routes of the WP REST API. By checking this nodes property, we can minimize the need for an external documentation to list all the supported routes. The routes are listed as individual objects. And the keys here signify an individual route. Inside the object are properties for that route, including the namespace it belongs to, its supported HTTP methods, and the respective endpoints. This args object lists all the arguments we can pass for this route. So when retrieving POST, we can set the context argument for setting the context of the return data. The page argument for referring to the current page of the POST collection, and the per page argument for setting the number of posts per page. So these are all the arguments you can post while reviewing POST. And inside each argument object are its properties, like whether or not this argument is required, its default value, and its description. The main thing to note when working with these arguments is their datatype. If the default property contains empty square brackets, that means that the argument type is and we can pass multiple values for it. And if there is a property name enum inside the argument object, it means that the argument is enumerated type. Enumerated types only accept predefined elements as valid values. If this is also not the case than the argument type is simple string. Similarly, these are the arguments that you can pass along the request for creating a POST. In the same way, you can view each individual route, its supported methods and their respective arguments. The last property here is this _links property and that holds an array of associated resources. This linking standard is based on HAL, or the hypertext application language. So that's all about retrieving some basic information about the API by sending a GET request to the index route. In the next lesson we will take a look at yet in the future, by which we can navigate the API, and that is by sending an options request. See you in the next video.

Back to the top