Python中使用requests库发送Http请求
最近在使用一个性能测试框架,locust,是用Python写的。模拟测试逻辑时,可用Python来编写需要进行测试的逻辑。
这篇文章讲讲如何使用 requests 这个库,以发送http请求。
Get请求
1 | url = 'http://localhost/api' |
Post请求
当我们使用json方式提交请求时,其实是不用在代码里面指定Content-type: applicaion/json
的,只需要使用json
参数就可以了。
1 | url = 'http://localhost/api' |
上传文件有点特殊,也不需要指定Content-type,使用files
参数即可。java服务端,使用MultipartFile来接收这个文件即可。
1 | url = 'http://localhost/api' |