django 使用requests请求第三方接口

get请求:

import requests
 
from django.http import JsonResponse
 
def get(request):
    url = 'xxx'  # 網址
    params = {'name': 'zhangsan', 'age': 18}
    response = requests.get(url=url, params=params)  # 用的是params
    return JsonResponse(response.text, safe=False)

post请求:

import requests
from json import dumps
from django.http import JsonResponse
 
def post(request):
    url = 'xxx'  # url網址
    data = {'name': 'zhangsan', 'age': 18}
    data = json.dumps(data)
    response = requests.post(url=url, data)  # 用的是data
    return JsonResponse(response.text, safe=False)

你可能感兴趣的:(Python)