Python response.status_代码为200,但response.content为空
我正在使用蝗虫对一些API进行负载测试,下面是蝗虫文件(locastfile.py)的样子:Python response.status_代码为200,但response.content为空,python,locust,Python,Locust,我正在使用蝗虫对一些API进行负载测试,下面是蝗虫文件(locastfile.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: pri
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\u code
打印为200,但response.content
恰好为空。当我使用Postman访问相同的API时,我在响应中看到了一个正确的响应体,这与预期的一样。这看起来像是一个奇怪的问题,它阻止我调用另一个依赖于get\u something()
方法的API,因为另一个API从get\u something()获取一些数据
方法作为输入。response.text
或response.\u content
而不是response.content
起作用。response.text
或response.\u content
而不是response.content
起作用。蝗虫的默认HTTP客户端是请求
请求为您提供了几种访问响应内容的方法:
- 要将内容解码为纯文本,请使用
response.text
- 要将内容解码为json,请使用
response.json()
- 要以二进制数据的形式访问内容,请使用
response.content
- 对于原始套接字响应,使用
response.raw
这在请求文档的“响应内容”部分有更详细的解释:蝗虫的默认HTTP客户端是请求 请求为您提供了几种访问响应内容的方法:
- 要将内容解码为纯文本,请使用
response.text
- 要将内容解码为json,请使用
response.json()
- 要以二进制数据的形式访问内容,请使用
response.content
- 对于原始套接字响应,使用
response.raw
这在请求文档的“响应内容”部分有更详细的解释:您是否使用不同的工具测试url?可能它总是返回空内容。@furas是的,我与Postman测试了相同的URL,它在响应正文中给出了正确的响应内容。我认为应该有
响应。\u content
。你能看看你得到了什么样的回应吗?回应。\内容
有效!response.text
,但我想知道为什么IDE在键入response.
后会在建议中显示内容。
。您是否使用不同的工具测试url?可能它总是返回空内容。@furas是的,我与Postman测试了相同的URL,它在响应正文中给出了正确的响应内容。我认为应该有响应。\u content
。你能看看你得到了什么样的回应吗?回应。\内容
有效!response.text
也是如此,但我想知道为什么IDE在键入response之后会在建议中显示内容。