zoukankan      html  css  js  c++  java
  • 重载的方式写Python的post请求

    #encoding=utf-8
    #__author__="Lanyangyang"

    import unittest
    import requests
    import json

    # This is a public post method.
    def test_public_post(self, url, path, params):

    # post request method
    response = requests.post(url=url + path, params=params)

    # Is the return status code 200?
    self.assertEqual(response.status_code, 200, msg="The status code is not 200")

    # Is the return message SUCCESS?
    response_text = requests.post(url=url + path, params=params).text

    # Convert response content into dictionary format.
    response_dict = json.loads(response_text)

    # Gets the value of the response message.
    response_message = response_dict['message']

    # Determine the value of message.
    self.assertEqual(response_message, 'SUCCESS', msg='The response message is not SUCCESS')

    # Print response text.
    print(response.text)

    # Output text in JSON format
    response2 = requests.post(url=url + path, params=params).json()
    r = json.dumps(response2, indent=2, sort_keys=True)

    # Print response text in JSON format
    print("The return information is as follows:")
    print(r)

    print("test_public_post call success")



    # This is a public post method, include headers
    def test_public_post(self, url, path, params,headers):

    # post request method
    response = requests.post(url=url + path, params=params,headers=headers)
    # Is the return status code 200?
    self.assertEqual(response.status_code, 200, msg="The status code is not 200")
    # Is the return message SUCCESS?
    response_text = requests.post(url=url + path, params=params,headers=headers).text
    # Convert response content into dictionary format.
    response_dict = json.loads(response_text)
    # Gets the value of the response message.
    response_message = response_dict['message']
    # Determine the value of message.
    self.assertEqual(response_message, 'SUCCESS', msg='The response message is not SUCCESS')
    #Print response text.
    print(response.text)

    # Output text in JSON format
    response2 = requests.post(url=url + path, params=params,headers=headers).json()
    r = json.dumps(response2, indent=2, sort_keys=True)

    #Print response text in JSON format
    print("Print response text")
    print(r)

    print("test_public_post call success")



    # This is a public post method, include headers,no param
    def test_public_post(self, url, path,headers):

    # post request method
    response = requests.post(url=url + path,headers=headers)
    # Is the return status code 200?
    self.assertEqual(response.status_code, 200, msg="状态码不是200")
    # Is the return message SUCCESS?
    response_text = requests.post(url=url + path, headers=headers).text
    # 将响应内容转换成字典格式
    response_dict = json.loads(response_text)
    # 获取response message字段的值
    response_message = response_dict['message']
    # 判断message的值
    self.assertEqual(response_message, 'SUCCESS', msg='返回值不是SUCCESS')
    print(response.text)

    # 以json格式输出返回文本
    response2 = requests.post(url=url + path,headers=headers).json()
    r = json.dumps(response2, indent=2, sort_keys=True)
    print("返回信息如下:")
    print(r)

    print("test_public_post调用成功")
  • 相关阅读:
    2020北航OO第二单元总结
    2020北航OO第一单元总结
    OO结课了,狂喜
    BUAAOO第三单元总结
    BUAAOO第二单元代码分析
    BUAAOO第一单元代码分析
    OO第四次博客作业
    OO第三次博客作业
    OO第二次博客作业
    OO第一次博客作业
  • 原文地址:https://www.cnblogs.com/lanyy/p/10420443.html
Copyright © 2011-2022 走看看