当前位置:网站首页>Restful design method
Restful design method
2022-06-24 18:13:00 【NeilNiu】
1、 domain name
Try to put API Deployed under a dedicated domain name
http://api.example.com
2、 edition
Should be API The version number of URL
http://www.example.com/app/1.0/foo
http://www.example.com/app/2.0/foo
3、 route
Express API The specific website of , Each website represents a resource , If you want to operate on any resource , What resources are used as paths
(1) Resources as web sites , Only nouns (books), Do not use verbs (add_book)
(2) Nouns of resources , You need to use the plural , Whether sub resources or all resources
for example
Get a single product : http://127.0.0.1:8080/AppName/rest/products/1
Get all products : http://127.0.0.1:8080/AppName/rest/products
for example
GET: /products Returns a list of all items
POST: /products Create a new product into a collection
4、HTTP Verb
frequently-used HTTP There are four verbs
GET(SELECT): Get resources from the server
POST(CREATE): Create a new resource on the server
PUT(UPDATE): Update resources on the server
DELETE(DELETE): Remove resources from server
for example :
GET: /zoo : List all zoos
POST: /zoos : Build a new zoo
GET: /zoos/ID: Get information about a designated Zoo
PUT /zoos/ID : Update the information of a designated Zoo ,
DELETE /zoos/ID : Delete a zoo
5、 Filtering information
When there are too many records ,API Parameters should be provided , Filter return results
for example :
?limit=10 Specify the number of returned records
?offset=10 Specify where to start the return record
?page=2&per_page=100 Specify page , And the number of records per page
6、 Status code
200 The server successfully returned
400 The request sent by the user is incorrect , The server has not been created
401 The user does not have permission ( token 、 user name 、 password )
403 Indicates that the user is authorized , But access is forbidden
406 The user request format is incorrect
7、 Error handling
If the status code is 4xx, The server should return an error message to the user , Generally speaking , The information returned will be error As a key name , The error message can be used as the key value
8、 Return results
For different operations , The results returned by the server to the user should conform to the following specifications
GET, /collection Returns a list of resource objects
GET,/collection/resource Return a single resource object
POST ,/collection Return the newly generated resource object
PUT ,/collection/resource Returns the complete resource object
DELETE /collection/resource Return an empty document
Write a RESTFul Style interface , Get one , Book details
http://127.0.0.1:8000/projects/books/5/
Registered routing
from django.urls import path
from django.conf.urls import url
from testdjango import views
urlpatterns = [
path('index', views.IndexView.as_view()),
url(r'^books/(?P<pk>\d+)/$', views.BookView.as_view()),
]class BookView(View):
def get(self, request, pk):
return JsonResponse({'title': 'test'})边栏推荐
- 国家出手了!对知网启动网络安全审查
- Industrial security experts talk about DDoS countermeasures from the perspective of attack and defense
- Issue 39: MySQL time class partition write SQL considerations
- How to use SEO to increase the inquiry volume?
- Service not found troubleshooting and resolution of error messages in the secondary development of the source code of the open source platform easydarwin
- Nacos cluster starts throwing set of SQL_ SELECT_ LIMIT is not support
- Operation and maintenance guide | cos back source setting practice
- Regression testing strategy for comprehensive quality assurance system
- Cloud MySQL importing cloud data warehouse PostgreSQL best practices
- [North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption
猜你喜欢

It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements

Ten excellent business process automation tools for small businesses

LC 300. Longest increasing subsequence
[golang] leetcode intermediate - jumping game & different paths

Regression testing strategy for comprehensive quality assurance system

How to start cloud native application development

(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines

Ten software development indicators for project managers

On software requirement analysis

Two micro service interviews where small companies suffer losses
随机推荐
How to start cloud native application development
Ten software development indicators for project managers
Three years of bug free, tips for improving code quality
Using easyjson to improve the efficiency of serialization transmission
Users of the Tiktok open platform are authorized to obtain the user's fan statistics and short video data
[golang] leetcode intermediate - jumping game & different paths
Gateway solves cross domain access
About swagger
A solution to the problem that the separator of WordPress title - is escaped as -
On the principle of cloud streaming multi person interaction technology
腾讯云荣获“可信云技术最佳实践-虚拟化”
电子元器件行业B2B电商市场模式、交易能力数字化趋势分析
Realize business development on behalf of small programs, and 99% restore the function of service category management in the background of official account
Leveldb source code analysis -- version management
Recommend 14 commonly used test development tools
Project Management Guide: tips, strategies and specific practices
EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
You don't know about this inspection platform. It's a big loss!
13 ways to reduce the cost of cloud computing