[英]python request get status code 200 but response.content is empty
[英]response.status_code is 200, but response.content is empty
我正在与Locust一起进行一些API的负载测试,下面是蝗虫文件(locustfile.py)的样子:
class MyTests(TaskSet):
def on_start(self):
print("Starting tests")
def get_something(self):
with self.client.get("some_baseuri" + "some_basepath", catch_response=True) as response:
print("Response code: {}".format(response.status_code))
print("Response body: {}".format(response.content))
@task(1)
def my_task(self):
self.get_something()
class WebsiteUser(HttpLocust):
task_set = MyTests
这是我触发测试的方式:
locust -f locustfile.py --no-web --clients=1 --hatch-rate=10 --host=http://127.0.0.1 --num-request=2 --print-stats --only-summary
问题是,在日志中, response.status_code
打印为200,但是response.content
恰好为空。 当我使用Postman调用相同的API时,在响应中可以看到预期的正确响应主体。 这看起来像一个奇怪的问题,使我无法调用依赖于get_something()
方法的另一个API,因为另一个API从get_something()
方法获取一些数据作为输入。
Locust的默认HTTP客户端是“请求”。
请求为您提供了几种访问响应内容的方法:
response.text
response.json()
response.content
response.raw
在“请求”文档的“响应内容”部分中对此进行了更详细的说明: http : //docs.python-requests.org/en/master/user/quickstart/#response-content
response.text
或response._content
而不是response.content
起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.