响应状态代码是200,但是响应.内容是空的

2024-05-15 19:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用rocast进行一些api的负载测试,下面是rocast文件(蝗虫档案.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

下面是我如何触发我的测试:

^{pr2}$

问题是,在日志中,response.status_code被打印为200,但是{}恰好是空的。当我使用Postman命中同一个API时,我在响应中看到了一个正确的响应体。这看起来像是一个奇怪的问题,它阻止我调用依赖于get_something()方法的另一个API,因为另一个API从get_something()方法获取一些数据作为输入。在


Tags: selfapiformattaskgetresponsedefcode
2条回答

蝗虫的默认HTTP客户端是请求。在

请求为您提供了几种访问响应内容的方法:

  • 要将内容解码为纯文本,请使用response.text
  • 要将内容解码为json,请使用response.json()
  • 要以二进制数据的形式访问内容,请使用response.content
  • 对于原始套接字响应,请使用response.raw

这在请求文档的“响应内容”部分中有更详细的解释:http://docs.python-requests.org/en/master/user/quickstart/#response-content

response.textresponse._content而不是{}起作用。在

相关问题 更多 >