小程序中如何使用 HTTP请求
首先我的最终目的是为了开发PythonDjango+微信小程序的校园应用,所以查看我这个系列就可以查看一些有用的内容
请求方法
请求参数
请求端信息
headers
cookies
request对象
请求方法:request-method
客户端信息-request-META属性
cookies-request-COOKIES属性
请求参数-request-QueryDict
HTTP应答
状态码-Response-status属性
应答内容-Response-content属性
内容格式-responr子类-JsonResponse,FileResponse
from django.http import HttpResponse,JsonResponse,FileResponse
def helloworld(request):
print("request method:",request.method)
print("Request META:",request.META)
print("request cookies",request.COOKIES)
print("request QueryDict",request.GET)
m = {
"status":"200",
"content":"ok",
"message":"Hello django response"
}
return JsonResponse(status=200,safe=False,data=m)
def weather(request):
if request.method == "GET":
city = request.GET.get("city")
data = juhe.weather(city)
return JsonResponse(data=data, status=200)
elif request.method == "POST":
receiverd_body = request.body
receiverd_body = json.loads(receiverd_body)
cities = receiverd_body.get("cities")
response_data = []
for city in cities:
result = juhe.weather(city)
result["city"] = city
response_data.append(result)
return JsonResponse(data=response_data, safe=False, status=200)