response.status_code 为 200,但 response.content 为空
response.status_code is 200, but response.content is empty
我正在使用 Locust 进行一些 API 的负载测试,下面是 Locust 文件 (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 时,我在响应中看到了预期的正确响应主体。这看起来像一个奇怪的问题,它阻止我调用另一个 API,它依赖于 get_something()
方法,因为另一个 API 从 get_something()
方法获取一些数据作为输入。
Locust 的默认 HTTP 客户端是 Requests。
请求为您提供了几种访问响应内容的方法:
- 将内容解码为纯文本,使用
response.text
- 将内容解码为json,使用
response.json()
- 要以二进制数据形式访问内容,请使用
response.content
- 对于原始套接字响应,使用
response.raw
这在请求文档的 "Response Content" 部分有更详细的解释:http://docs.python-requests.org/en/master/user/quickstart/#response-content
我正在使用 Locust 进行一些 API 的负载测试,下面是 Locust 文件 (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 时,我在响应中看到了预期的正确响应主体。这看起来像一个奇怪的问题,它阻止我调用另一个 API,它依赖于 get_something()
方法,因为另一个 API 从 get_something()
方法获取一些数据作为输入。
Locust 的默认 HTTP 客户端是 Requests。
请求为您提供了几种访问响应内容的方法:
- 将内容解码为纯文本,使用
response.text
- 将内容解码为json,使用
response.json()
- 要以二进制数据形式访问内容,请使用
response.content
- 对于原始套接字响应,使用
response.raw
这在请求文档的 "Response Content" 部分有更详细的解释:http://docs.python-requests.org/en/master/user/quickstart/#response-content