Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.2 OPTIONS Requests

If we need to learn about a certain route, then sending an OPTIONS request is the easiest way. In this lesson, I'll show you how to explore individual routes, their endpoints, and their supported arguments.

4.2 OPTIONS Requests

Hey folks and welcome back to the course Introducing the WP REST API. I'm your instructor Bilal Shahid. In the previous lesson we took a look at retrieving fundamental information about the API by sending a get request to the index route. This information contains things such as the site name, its description, supported authentication methods and all the supported routes. But this information can be a bit confusing if you just want to exclude a particular route. For excluding a particular route, its methods and its supported arguments, it's more feasible to send an options request to get particular route. Let's begin by sending an options request to the posts route. This returns an object that contains various properties like the namespace this route belongs to, its support methods and its supported endpoints. It's clear from the response that this route belongs to the default wp/v2 namespace and its supported methods are GET and POST. That is, we can use this route to retrieve and create posts. For retrieving posts, the get method is used and its supported arguments are context, page per page, search, etc. The format of this data is exactly the same as we saw in the previous lesson when sending a GET request to the index route. And for creating posts, the POST method is used, and for creating a post you can use all of these arguments that include date, date_gmt, password, slug and status. This is very much similar to what you saw in the previous lesson when sending a GET request to the index route. In addition to these properties, the response also contains the schema property that defines the schema of the current resource. This schema is basically a documentation for all the properties supported by the resource like its description, its data type, its format and context. The schema used in the WP REST API is based on the draft four of the JSON schema specifications. So, that's all about navigating the API by sending an options request. In these two lessons, we looked at ways for exploring the API by sending either GET request to the index route or by sending an options request to a particular route. It's up to you what method you use. If you need to list all the supported routes for the API, you can send a GET request to the index route, but if you need to look at a particular route more closely, you can send an options request for that particular route. In the next lesson, we will use their techniques learned so far to retrieve data with the help of WP REST API. See you in the next video.

Back to the top