Python接口测试-使用requests模块发送post请求

本篇主要记录下使用python的requests模块发送post请求的实现代码.

#coding=utf-8
import unittest
import requests

class PostTest(unittest.TestCase):

    def setUp(self):
        host = 'https://httpbin.org/'
        endpoint = 'post'
        self.url = ''.join([host, endpoint])

    def testPost(self):
        params = {'show_env':'1'}
        json = {
            'info': {'show_env': '2', 'sex': 'nv'},
            'code': 200,
            'a': 'hello', 'b': 'nihao',
            'files' : {'file': ('test.txt', 'hello')},
            'data': [{'name': 'zhangsan', 'id': '123'}, {'name': 'lisi', 'id': '125'}],
            'id': 1900
        }

        r1 = requests.post(self.url,params=params,json=json)
        resp1 = r1.json()
        print(resp1)
        connect = resp1['headers']['Connection']
        self.assertEqual(connect, 'close')

    def tearDown(self):
        pass

if __name__=='__main__':
    unittest.main()
原文地址:https://www.cnblogs.com/feiyueNotes/p/7857977.html