Next lesson playing in 5 seconds

  • 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.

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.

Back to the top