简体   繁体   中英

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:

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. When I hit the same API using Postman, I see a proper response body in the response as expected. 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.

Locust's default HTTP client is Requests.

Requests gives you several ways to access response content:

  • for decoding the content as plain text, use response.text
  • for decoding the content as json, use response.json()
  • for accessing the content as binary data, use response.content
  • for the raw socket response, use 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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