简体   繁体   English

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

[英]response.status_code is 200, but response.content is empty

I'm working with Locust for load testing a few APIs, below is what the locust file (locustfile.py) looks like: 我正在与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

Here's how I trigger my tests: 这是我触发测试的方式:

locust -f locustfile.py --no-web --clients=1 --hatch-rate=10 --host=http://127.0.0.1 --num-request=2 --print-stats --only-summary

The problem is, in logs, response.status_code is printed as 200, but response.content happens to be empty. 问题是,在日志中, response.status_code打印为200,但是response.content恰好为空。 When I hit the same API using Postman, I see a proper response body in the response as expected. 当我使用Postman调用相同的API时,在响应中可以看到预期的正确响应主体。 This looks like a strange issue which blocks me from calling another API which is dependent on the get_something() method since the other API takes some data from get_something() method as an input. 这看起来像一个奇怪的问题,使我无法调用依赖于get_something()方法的另一个API,因为另一个API从get_something()方法获取一些数据作为输入。

Locust's default HTTP client is Requests. Locust的默认HTTP客户端是“请求”。

Requests gives you several ways to access response content: 请求为您提供了几种访问响应内容的方法:

  • for decoding the content as plain text, use response.text 要将内容解码为纯文本,请使用response.text
  • for decoding the content as json, use response.json() 要将内容解码为json,请使用response.json()
  • for accessing the content as binary data, use response.content 要以二进制数据形式访问内容,请使用response.content
  • for the raw socket response, use response.raw 对于原始套接字响应,请使用response.raw

This is explained in much more detail under the "Response Content" section of the Requests docs: http://docs.python-requests.org/en/master/user/quickstart/#response-content 在“请求”文档的“响应内容”部分中对此进行了更详细的说明: 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