- Overview
- Transcript
4.4 Create, Update, and Delete Data
In this lesson, I'll show you how to create, update, and delete data with the WP REST API. We'll look at a few different methods to pass data to the server including JSON, form data, and URL parameters.
1.Introduction1 lesson, 01:17
1.1Introduction01:17
2.How REST APIs Work3 lessons, 15:33
2.1HTTP Methods and Response Codes04:34
2.2Routes and Endpoints03:43
2.3JSON07:16
3.Set Up the Environment2 lessons, 08:22
3.1Set Up a Local WordPress Install04:16
3.2Installing the HTTP Client Postman04:06
4.Exploring the WordPress REST API4 lessons, 21:43
4.1The Index Route05:59
4.2OPTIONS Requests03:26
4.3Retrieve Data With GET Requests05:28
4.4Create, Update, and Delete Data06:50
5.The WordPress REST API in Action2 lessons, 16:47
5.1Retrieve Data With jQuery08:02
5.2Create Data With the WP REST API and jQuery08:45
6.Conclusion1 lesson, 00:59
6.1Conclusion00:59
4.4 Create, Update, and Delete Data
Hey folks, and welcome back to the course introducing the WP REST API. I'm the instructor Bilal Shahid. In the previous lesson, we learned about retrieving posts with the WP Rest API and Postman client. In the current lesson, we will learn about creating, updating and deleting data with the WP REST API. We will start by creating a simple post. The route for creating a post is /wp-json /wp/v2/posts. And we will start that HTTP method to post using this drop down. Since we are creating a post, we need to pull some data along the request. This data can be pass in any of the three formats, that is by using JSON notation, bypassing it as URL parameters or using FormData. We will first take a look at passing data using your parameters. But before sending a post request, keep in mind that creating, updating and deleting data requires authentication. And we will use the SQL authentication plugin we had installed in a previous lesson of this course. You can enter the credentials using the authorization tab and then by selecting the basic auth option from the drop down Now, you can set the data as URL parameters. Hence, we have specified two parameters for the title and the content of the post. Send the request. And if the request is successful, the server will return the newly created post object as JSON data that will show up here at the bottom. As we can see in the response object the status of this newly created post is draft. We can update the status of this post by sending another post or put request to this individual post route by using its ID. We will specify this status as publish and send a request. So the request has updated the status of this particular post, as we can see in the response object returned by the server. Next, we can create a post using the JSON notation. For that purpose, the need to pass the JSON object by navigating into the body type selecting the raw radio button and then selecting the JSON content type from the drop down. In this text are, we can specify the JSON object that contains a tittle, content and the status of this post Send the request and the server will return the 201 status code, specifying that the post has been created successfully. And again, we send a put request to the route of this individual post to update it. So, I'll update the status back to draft using the simple JSON object. And the post has been updated successfully. And for the last method of creating post by using FormData, we can switch to the body type, select the form-data radio button and simply fill these key value fields with the arguments. While creating these three posts, we use three arguments for the content, title, and the status of the post. If we try to send an empty post request to create a post, the server will run a 400 bad request error, indicating that any one of the content, title, or xserve argument should be present in the request body And as we have learned in the previous lessons, you can view all the arguments supported for creating a post by sending in options request with the post route. Similarly, you can look at other routes for creating resources like users comments, or categories as working with them is quite similar to working with posts. The next thing we look at is uploading media. And for that purpose,we need to use FormData, so we will switch to the body tab, select the FormData radio button, and then select the type of this form field to be a file. The name of the field should be file and we can browse this the from our hard drive. Press the send button and the server will load the newly created media object. And lastly, we can delete a particular resource by sending a delete request to the route of that individual resource. So, if we need to delete a post with an ID of say, ten, we can send a delete request and that post will be moved to crashed. If we need to delete a post permanently, we can pass along the force argument like this. It's worth mentioning here, that some of the resources including tags, categories, and users, require you to pass the force argument since they do not support crashing. So, if I try to delete a user having an ID of two without passing the force argument, the server will return an error. And by specifying the force argument, the resource will be deleted permanently. So that's all about creating, updating and deleting resources using the WP REST API and Postman. Starting from the next section, we will create two simple projects for listing and creating post with the WP REST API and G query.