繁体   English   中英

response.status_code为200,但response.content为空

[英]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
  • 要将内容解码为json,请使用response.json()
  • 要以二进制数据形式访问内容,请使用response.content
  • 对于原始套接字响应,请使用response.raw

在“请求”文档的“响应内容”部分中对此进行了更详细的说明: http : //docs.python-requests.org/en/master/user/quickstart/#response-content

response.textresponse._content而不是response.content起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM