学API接口测试,致富植发(X)——RESTful接口
仰望高级玩家:
https://www.jianshu.com/p/65ab865a5e9f
https://blog.csdn.net/qq_27026603/article/details/82012277
看了看,想了想,抄了抄,还是太高深了。。。
我从简 orz
一、定义
RESTful接口只是指一种REST风格的接口,HTTP就是该架构风格的一个典型应用。
二、特点:
1. 使用url表示资源
2. 使用请求方法表示对资源的操作
-
GET 查询
-
POST 新增
-
PUT 更新
-
DELETE 删除
传统风格的接口(使用动词):
查询学生信息:http://127.0.0.1:8000/student/query?name=tom
新增学生信息:http://127.0.0.1:8000/student/add?name=tom
更新学生信息:http://127.0.0.1:8000/student/update?name=tom
删除学生信息:http://127.0.0.1:8000/student/delete?name=tom
REST风格的接口(使用名词<name>:):
查询学生信息:http://127.0.0.1:8000/student/tom
新增学生信息:http://127.0.0.1:8000/student/tom
更新学生信息:http://127.0.0.1:8000/student/tom
删除学生信息:http://127.0.0.1:8000/student/tom
来呀来呀,敲下代码呀快活呀~
1、创建工程、创建app(这个方式创建的app会自动添加到settings.py)
2、数据库绝对路径改成相对路径
3、创建学生数据库
4、创建学生表
5、先定义个接口试试水
from django.shortcuts import render
from django.http import *
# Create your views here.
def get_student_info(request,name):
if request.method=="GET":
print("name:",name)
return JsonResponse({"result": 1,"msg": "success"})
else:
return JsonResponse({"result": 2,"msg": "fail"})
6、映射路径
from django.contrib import admin
from django.urls import path
from student.views import *
urlpatterns = [
path("student/<name>",get_student_info),
path('admin/', admin.site.urls),
]
7、启动服务器
8、浏览器访问
9、先在数据库添加一条数据
10、完善接口
from django.shortcuts import render
from django.http import *
from student.models import Student
# Create your views here.
def get_student_info(request,name):
# 查询学生信息
if request.method=="GET":
print("name:",name)
s=Student.objects.filter(name=name)
if len(s)==0:
content={"result": 1,"msg": "query fail","data": None}
else:
s=s[0]
content={"result": 0,"msg": "query success","data":
{"name": s.name,"age": s.age,"sex": s.sex,"classid":s.s_class}}
return JsonResponse(content)
# 查删除学生信息
elif request.method=="DELETE":
return JsonResponse({"result": 0,"msg": "delete success"})
# 修改学生信息
elif request.method=="PUT":
return JsonResponse({"result": 0,"msg": "update success"})
else:
return HttpResponseNotAllowed(["GET","DELETE","PUT"])
def add_student_info(request):
# 新增学生信息
if request.method!="POST" :
return HttpResponseNotAllowed(["POST"])
return JsonResponse({"result": 0,"msg": "success"})
from django.shortcuts import render
from django.http import *
from student.models import Student
# Create your views here.
def get_student_info(request,name):
# 查询学生信息
if request.method=="GET":
print("name:",name)
s=Student.objects.filter(name=name)
if len(s)==0:
content={"result": 1,"msg": "query fail","data": None}
else:
s=s[0]
content={"result": 0,"msg": "query success","data":
{"name": s.name,"age": s.age,"sex": s.sex,"classid":s.s_class}}
return JsonResponse(content)
# 查删除学生信息
elif request.method=="DELETE":
ret=Student.objects.filter(name=name).delete()
if ret[0]==1:
return JsonResponse({"result": 0,"msg": "delete success"})
else:
return JsonResponse({"result": 2,"msg": "delete fail"})
# 修改学生信息
elif request.method=="PUT":
return JsonResponse({"result": 0,"msg": "update success"})
else:
return HttpResponseNotAllowed(["GET","DELETE","PUT"])
def add_student_info(request):
# 新增学生信息
if request.method!="POST" :
return HttpResponseNotAllowed(["POST"])
return JsonResponse({"result": 0,"msg": "success"})
11、映射接口
"""web_rest URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from student.views import *
urlpatterns = [
path("student/<name>",get_student_info),
path("student", add_student_info),
path('admin/', admin.site.urls),
]
上一篇: linux下ptp性能测试
下一篇: Git(2)-Git常用的操作和概念