hualinux dj3 2.2: RESTful API description and examples

table of Contents

1. Review before and after the appearance of restful

1.1 Comparison before and after the emergence of restful

1.2 restful api example

1.2.1 Example requirements

1.2.1 Use put to modify

1.2.2 Use patch to modify

1.2.3 The difference between put and patch

Second, the rest understanding of Django REST framework (drf)


I talked about restful api in " What is an API interface (a must-see for newbies!) " in the column " Introduction 3 Programming Concepts ", but I didn't give an example. So this article will give an example for further explanation.

1. Review before and after the appearance of restful

1.1 Comparison before and after the emergence of restful

I still take the project established in " Connecting to mysql8.0 Database " as an example. Do a CURL operation explanation

The following stu means the student student, sid is the student number, which is a variable to fill in the specific value, we add, delete and change the check operation on the condition of the student number

Operation before Restful: <Using pathinfo mode for url>
http://127.0.0.1/stu/query/<sid>    GET submission: Indicates to query student information with student ID sid
http://127.0.0.1/stu/save                POST submission: means adding a new student
http://127.0.0.1/stu/update/<sid>     PUT Tip: Modify student information with student ID as sid
http://127.0.0.1/stu/delete/<sid>     GET or POST submission: means to delete student information with student ID sid
RESTful usage:
http://127.0.0.1/stu/<sid>       GET submission: means to query student information with student ID sid
http://127.0.0.1/stu/                POST submission: means to add a new student
http://127.0 .0.1/stu/<sid>       PUT submission: Indicates to modify all information of the student whose student ID is sid (all updated)
http://127.0.0.1/stu/<sid>       PATCH submission: Indicates to modify the part of the student whose student ID is sid Information (partial update)
http://127.0.0.1/stu/<sid>       DELETE submission: means to delete student information whose student ID is sid
Note: The difference between the put and patch methods above. If you use put, you need to submit all the fields of the table. If you use patch, you only need to submit part of it.
PS: If we have learned some development basics, we all know that get submission means that the webpage is opened normally, and the normal writing is http://127.0.0.1:8000/sid?stuSid=001
And put? The number is changed to a slash, the variable name is omitted, and it becomes sid/<variable value>. This way is called pathinfo mode of url

Except for the get access parameters above, which are written on the url address, the other method requests are not written in the url, but are placed in the body body and submitted to the specified URL when submitted.

1.2 restful api example

1.2.1 Example requirements

Let me give an example, taking the student list of the previous students. Let me take a look at the first 5 pieces of student information, as shown in the figure below:

I found that Zhang San’s language score was wrong. If you want to change it to 81, you can use put. You can use professional front-end axios such as vue or ajax of jquery to submit put. For convenience, I don’t write the front-end and use postman directly. Tool for testing, I use json submission, generally the front and back ends are separated using json, if you don’t understand json, you can take a look at the json tutorial .

1.2.1 Use put to modify

I first use put to modify the student's grades, which is represented by json

{"id":1,"sid":"001","name":"张三","yuWen":"81.0","shuXue":"65.0"}

Use the put method to  submit the above json to  http://127.0.0.1:8000/stu/001/ . The screenshot with postman is as follows:

Use sql to check the effect:

==, I only modify the language scores, don't I just write {"yuWen":"81.0"} directly in json? !

Then we tried it and found that an error was reported, as follows:

1.2.2 Use patch to modify

I think the above design is unreasonable, I only want to submit the changes that need to be modified, and the others remain unchanged, okay? ! Then use patch. Let's change the PUT method above to patch and try it. In order to see the effect, I changed the Chinese score to 72 points. The effect is shown in the figure:

It was found that no errors were reported, and the modification was successful. Take a look at the executed SQL and found that it was also modified to 72 points, as shown in the following figure:

1.2.3 The difference between put and patch

So do you understand the difference between put and patch? Generally, PUT submission is to advance the values ​​of all fields, and patch only needs to advance the values ​​of some fields.

So PUT is called full update, and PATCH is called partial update.

Second, the rest understanding of Django REST framework (drf)

The rest in Django REST framework (drf for short) is the restful style. Because it is an api and conforms to the rest style, it is called restful api like the one mentioned above.

We are going to use almost all methods of http, so that we can make full use of the advantages of http.