Request模块收发json 数据
requests.get.text
返回文本字符串
api
def api(request):
if request.method == 'POST':
print(request.POST)
k1, k2 = request.POST.get('k1')[0], request.POST.get('k2')[0]
return JsonResponse({'status': 200, 'msg': '接收到了'}, json_dumps_params={'ensure_ascii': False})
if request.method == 'GET':
host_list = ['xmaster', 'xminion']
return JsonResponse(host_list, safe=False) # JsonResponse如果返回列表, 需要加上safe=False
客户端
url = 'http://127.0.0.1:8000/api/'
response = requests.get(url=url) # ["xmaster", "xminion"] <class 'str'>
print(response.text, type(response.text))
输出结果
["xmaster", "xminion"] <class 'str'>
requests.get.json
返回原本的数据类型, 比如说在服务端序列化前的数据类型是列表, 则最后返回列表
api
def api(request):
if request.method == 'POST':
print(request.POST)
k1, k2 = request.POST.get('k1')[0], request.POST.get('k2')[0]
return JsonResponse({'status': 200, 'msg': '接收到了'}, json_dumps_params={'ensure_ascii': False})
if request.method == 'GET':
host_list = ['xmaster', 'xminion']
return JsonResponse(host_list, safe=False) # JsonResponse如果返回列表, 需要加上safe=False
客户端
import requests
url = 'http://127.0.0.1:8000/api/'
response = requests.get(url=url)
print(response.json(), type(response.json()))
输出结果
['xmaster', 'xminion'] <class 'list'>
requests.post
客户端发送字典套字典数据的时候, 服务端最后接收的数据是外层的字典,
request.POST内部构成为 {外层字典的key: 内层字典的key}
request.body内部构成为 b'外层字典的key=内层字典的key&外层字典的key=内层字典的key'
注意: 只有request.body的数据构成是上面的格式, request.POST才能够接收到值
客户端
import requests
url = 'http://127.0.0.1:8000/api/'
info = {'k1': {'ens33': '网卡坏了'}, 'k2': {'ens22': '192.168.1.1'}}
response = requests.post(url=url, data=info)
api
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
# Create your views here.
def api(request):
if request.method == 'POST':
print(request.POST) # <QueryDict: {'k1': ['ens33'], 'k2': ['ens22']}>
print(request.body) # b'k1=ens33&k2=ens22'
return JsonResponse({'status': 200, 'msg': '接收到了'}, json_dumps_params={'ensure_ascii': False})
if request.method == 'GET':
host_list = ['xmaster', 'xminion']
return JsonResponse(host_list, safe=False)
输出结果
<QueryDict: {'k1': ['ens33'], 'k2': ['ens22']}>
b'k1=ens33&k2=ens22'
解决方案: 使用json进行传递数据
方法1: 使用requests.post中的json选项来做
客户端
import requests
url = 'http://127.0.0.1:8000/api/'
info = {'k1': {'ens33': '网卡坏了'}, 'k2': {'ens22': '192.168.1.1'}}
response = requests.post(url=url, json=info)
api
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
import json
def api(request):
if request.method == 'POST':
print(request.POST)
print(request.body)
json_str = request.body.decode('utf-8')
ret = json.loads(json_str)
print(ret, type(ret))
return JsonResponse({'status': 200, 'msg': '接收到了'}, json_dumps_params={'ensure_ascii': False})
if request.method == 'GET':
host_list = ['xmaster', 'xminion']
return JsonResponse(host_list, safe=False)
方法2: 使用python的模块json来手动做
客户端
import requests
import json
url = 'http://127.0.0.1:8000/api/'
info = {'k1': {'ens33': '网卡坏了'}, 'k2': {'ens22': '192.168.1.1'}}
response = requests.post(url=url, data=json.dumps(info).encode('gbk'))
api
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
import json
# Create your views here.
def api(request):
if request.method == 'POST':
print(request.POST)
print(request.body)
json_str = request.body.decode('gbk')
ret = json.loads(json_str)
print(ret, type(ret))
return JsonResponse({'status': 200, 'msg': '接收到了'}, json_dumps_params={'ensure_ascii': False})
if request.method == 'GET':
host_list = ['xmaster', 'xminion']
return JsonResponse(host_list, safe=False)