Python Django + postman get 与post 处理

  1. 首先使用 post man 制作测试脚本, 新建http request 后可以点击generate code产生 Python代码

  2. get 的代码
    import requests
    url = “http://127.0.0.1:8001/tst/tst_url/”

    querystring = {“username”:“admin”,“password”:“qwertasdfg”}
    headers = {
    ‘cache-control’: “no-cache”,
    ‘postman-token’: “793210aa-2623-faf3-4225-4545aefabd3a”
    }
    response = requests.request(“GET”, url, headers=headers, params=querystring)
    print(response.text)

  3. post 的代码
    import requests
    url = “http://127.0.0.1:8001/tst/tst_url/”
    querystring = {“username”:“admin111我”,“password”:“qwertasdfg111”}
    payload = “{“form_username”:“admin1”,“form_password”:“qwertasdf1go我wo”}”
    headers = {
    ‘header1’: “XXXXXx”,
    ‘header2’: “YYYYY”,
    ‘authorization’: “Basic YWRtaW46cXdlcnRhc2RmZw==”,
    ‘cache-control’: “no-cache”,
    ‘postman-token’: “d5e34786-a825-e303-80f9-8bea6c837a8b”
    }
    response = requests.request(“POST”, url, data=payload, headers=headers, params=querystring)

    print(response.text)

  4. post man 的断言编写示例:

    tests[“Status code is 200”] = responseCode.code === 200;
    tests[“Response time is less than 200ms”] = responseTime < 200;
    tests[“Body matches string”] = responseBody.has(“000001”);

    var jsonData = JSON.parse(responseBody);
    tests[“rep_code”] = jsonData.rep_code === “000001”;
    tests[“url_password”] = jsonData.url_password === “----”;

  5. Django 中对 get/post 请求的处理代码
    @csrf_exempt
    def tst_url(request):
    rtn_code = “999999”

     logger.info("-------------------------- started, username=%s --------------------------", request.user.username)
    
     rsp_dict = {}
    
     if request.method == 'GET':
    
         #"http://127.0.0.1:8001/tst/tst_url/?para1=a¶2=b"
         logger.info("get method start")
    
         concat = request.GET
         postBody = request.body
         print(concat)
         print(type(postBody))
         print(postBody)
    
         print(concat.get('para1'))
    
         print(request.GET.get('username'))
         print(request.GET.get('password'))
    
    
         return HttpResponse("000000")
    
     elif request.method == "POST":
         logger.info("post method")
    
         #先从request POST 对象中获取内容
         concat = request.POST
         postBody = request.body
         print(concat)
         print(type(postBody))
         print(postBody)
         print(request.get_host())
         print(request.path)
         print(request.get_full_path())
         print(concat.getlist('username'))
    
         #获取URL中的参数
         print(request.GET.get('username', "---"))
         print(request.GET.get('password', "---"))
         print(request.POST.get('username', "---"))
         print(request.POST.get('password', "---"))
    
         rsp_dict['url_username'] = request.GET.get('username', "---")
         rsp_dict['url_password'] = request.GET.get('password', "---")
    
         #获取header
         print(request.META.get("CONTENT_LENGTH"))
         print(request.META.get("CONTENT_TYPE"))
         print(request.META.get("HTTP_ACCEPT"))
         print(request.META.get("HTTP_ACCEPT_ENCODING"))
         print(request.META.get("HTTP_ACCEPT_LANGUAGE"))
         print(request.META.get("HTTP_HOST"))
         print(request.META.get("HTTP_REFERER"))
         print(request.META.get("HTTP_USER_AGENT"))
         print(request.META.get("QUERY_STRING"))
         print(request.META.get("REMOTE_ADDR"))
         print(request.META.get("REMOTE_HOST"))
         print(request.META.get("REMOTE_USER"))
         print(request.META.get("REQUEST_METHOD"))
         print(request.META.get("SERVER_NAME"))
         print(request.META.get("SERVER_PORT"))
         #自定义请求头
         print(request.META.get("HTTP_HEADER1"))
         print(request.META.get("HTTP_HEADER2"))
         print(request.META.get("Authorization"))
    
         rsp_dict['HTTP_HEADER1'] = request.META.get("HTTP_HEADER1")
         rsp_dict['HTTP_HEADER2'] = request.META.get("HTTP_HEADER2")
    
         #从form body 中以json方式获取参数
         import json
         str = postBody.decode(encoding='utf-8')
         print(str)
         sjson = json.loads(str)
         print(sjson)
         print(sjson["form_username"])
         print(sjson["form_password"])
    
         rsp_dict['form_username'] = request.META.get("form_username")
         rsp_dict['form_password'] = request.META.get("form_password")
    
         rsp_dict['rep_code'] = "000001"
    
         res_json = json.dumps(rsp_dict).encode(encoding='utf-8')
    
         try:
             #return HttpResponse("000001" + request.META.get("HTTP_HEADER1"))
             return HttpResponse(res_json)
    
         except Exception as e:
             print(e)
    

你可能感兴趣的:(Python,postman,get,post,http)