Restful specifications (9 kinds)

This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

Thank you for meeting you. I’m Y Dazhuang

By Y Dazhuang Link: juejin.cn/user/756923… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the article and pictures from the Internet, if you have any questions please contact me

🌊🌈

1. General interface development

urlpatterns = [
    # url(r'^admin/', admin.site.urls),
    url(r'^get_order/', views.get_order),
    url(r'^add_order/', views.add_order),
    url(r'^del_order/', views.del_order),
    url(r'^update_order/', views.update_order),
]

def get_order(request) :
	return HttpResponse(' ')

def add_order(request) :
	return HttpResponse(' ')

def del_order(request) :
	return HttpResponse(' ')

def update_order(request) :
	return HttpResponse(' ')
Copy the code

2. Restful specification development (9 kinds)

Do different operations based on method

FBV

urlpatterns = [ url(r'^order/', views.order), ] def order(request): if request.method == 'GET': Return HttpResponse(' get order ') elif request.method == 'POST': return HttpResponse(' create order ') elif request.method == 'PUT': Elif request.method == 'DELETE': return HttpResponse(' DELETE order ')Copy the code

CBV

urlpatterns = [ url(r'^order/', views.OrderView.as_view()), ] class OrderView(View): Def post(self,request,*args,**kwargs): return HttpResponse(self,request,*args,**kwargs) Return HttpResponse(' create order ') def put(self,request,*args,**kwargs): Def delete(self,request,*args,**kwargs): return HttpResponse(' delete order ') def delete(self,request,* *kwargs): return HttpResponse(' delete order ')Copy the code

2.1 the domain name

The API should be deployed under a private domain as much as possible.

https://api.example.com
Copy the code

If you determine that the API is simple and will not be extended further, consider placing it under the main domain.

https://example.org/api/
Copy the code

Version 2.2 (Versioning)

You should put the VERSION number of the API in the URL.

http://www.example.com/app/1.0/foo
Copy the code

1) AcceptHeaderVersioning

The Accept passed in the request header carries version version information

GET /bookings/ HTTP/1.1 Host: example.com Accept: application/json; Version = 1.0Copy the code

2) URLPathVersioning(recommended)

The URL path contains version information

urlpatterns = [
    url( r'^(?P<version>(v1|v2))/bookings/$', bookings_list,name='bookings-list' ),
    url( r'^(?P<version>(v1|v2))/bookings/(?P<pk>[0-9]+)/$',bookings_detail,name='bookings-detail')
]
Copy the code

2.3 the path

The path, also known as the endpoint, represents the specific URL of the API. Each url represents a resource.

(1) As a website, resources can only have nouns, not verbs, and the nouns used are often corresponding to the table name of the database.

(2) Nouns in API should be plural. Either subresources or all resources.

For example, an API to get a product can be defined like this

2.4 HTTP

Type of operation on a resource

GET
POST 
GET 
PUT
PATCH 
DELETE
GET 
DELETE 
Copy the code

2.5 Filtering Information

If there are many records, the server cannot return them all to the user. The API should provide parameters that filter the return results.

Here are some common parameters.

? Limit =10: Specify the number of records to return? Offset =10: Specifies the start of the return record. ? Page =2&per_page=100: Specifies the number of pages and the number of records per page. ? Sortby = name&ORDER = ASC: Specifies by which attribute the return results are sorted, and in what order. ? Animal_type_id =1: Specifies a filter conditionCopy the code

2.6 status code

The status code and prompt message returned by the server to the user are as follows (the corresponding HTTP verb is in square brackets).

200, 300, 400, 500, whateverCopy the code

2.7 Error Handling

If the status code is 4XX, the server should return an error message to the user. In general, error is returned as the key name and the error message as the key value.

2.8 Result

For example, resF's ResponseCopy the code

2.9 other

The data format returned by the server should be JSON rather than XML.

3. My understanding of restful API specifications

It's a specification, define some specifications, make it easier for us to distinguish when we write API, make it easier for our background to process, make it easier for the front end to record THE URL, in other words, let the URL can reflect the operation of the API, so that the returned information can experience the operation of the interface.Copy the code

@property

class pop: def __init__(self): self.a = "abc" @property def user(self): Return a p = pop() p.user #Copy the code