当前位置:网站首页>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'})边栏推荐
- 投资理财产品的钱能随时取出来吗?
- 2. Leveldb design principle -- LSM
- 这个巡检平台你还不知道,真是亏大了!
- [JS Framework] Failed to execute the callback function:
- Design topic: MATLAB cellular automata personnel evacuation
- Leveldb source code analysis -- writing data
- Top ten popular codeless testing tools
- 你知道CMDB吗?
- Three simple steps to quickly complete order data processing through workflow (ASW)
- [NLP] 3 papers on how Stanford team builds a better chat AI
猜你喜欢

Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs

How to create simple shapes in illustrator 2022

Skills of writing test cases efficiently

Eight recommended microservice testing tools

Mcu-08 interrupt system and external interrupt application

Nine practical guidelines for improving responsive design testing

How can an enterprise successfully complete cloud migration?
What if the database table structure changes? Smartbi products support one click synchronization

Regression testing strategy for comprehensive quality assurance system

Specification for self test requirements of program developers
随机推荐
Use py-mysql2pgsql to synchronize MySQL data to Greenplum
Noi Mathematics: solution of quadratic congruence equation
Leveldb source code analysis -- writing data
LC 300. Longest increasing subsequence
Can the money invested in financial products be withdrawn at any time?
腾讯云TCS:面向应用的一站式PaaS 平台
持续助力企业数字化转型-TCE获得国内首批数字化可信服务平台认证
Leetcode topic [array] -46- full arrangement
EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
TCE入围2020年工信部信创典型解决方案
Introduction to yottastore, a new generation of storage engine for VPC TCE cos
Three simple steps to quickly complete order data processing through workflow (ASW)
Three indicators to help you measure the effectiveness of digital transformation
Software testing methods: a short guide to quality assurance (QA) models
Etching process flow for PCB fabrication
Mariana Trench, Facebook's open source code analysis tool
(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now