首页 > 解决方案 > 如何打印 Locust 响应 (JSONDecodeError)

问题描述

我有一个烧瓶应用程序,如下所示:(请注意,为了这个问题,我已经简化了它)

@app.route("/app/ent/", methods=['POST'])
def methodpost():
    req_data = request.get_json()
    msg = req_data['msg']
    output = jsonify(msg=msg)
    return output

然后为此,我有一个如下所示的蝗虫文件:

from locust import HttpLocust, TaskSet, task, between


class MyClass(TaskSet):

    @task(1)
    def send_post(self):
        self.client.headers['Content-Type'] = "application/json"
        response = self.client.post("/app/ent/", json=
        {
            "msg": "test mesg example"
        })

        #temp
        json_response_dict = response.json()
        msg = json_response_dict['msg']
        print("Post nessage returned is " + msg)



class MyTest(HttpLocust):
    task_set = MyClass
    wait_time = between(0.5, 3.0)
    host = "http://localhost:5000"

我开始蝗虫如下:

蝗虫-f locust_myExample.py

然后,当我使用 UI 运行它时,出现以下错误:

    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

知道如何打印烧瓶应用程序返回的“味精”吗?

但是,为了确保它有效,当我使用 cURL 进行手动测试时,它返回“msg”

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"msg":"test mesg example"}' \
  http://localhost:5000/app/ent

测试消息示例

标签: pythonflasklocust

解决方案


解决方案是:

返回的响应不是 json,当它最终以 JSON 形式返回时,它可以工作。缺少这一行:

output = jsonify(msg=msg)

推荐阅读