【问题标题】:How to print a Locust response (JSONDecodeError)如何打印 Locust 响应 (JSONDecodeError)
【发布时间】:2020-04-13 19:08:57
【问题描述】:

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

@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"

我启动蝗虫如下:

locust -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)

知道如何打印烧瓶应用返回的“msg”

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

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

测试消息示例

【问题讨论】:

  • 错误信息在这里我认为是不言自明的:你没有在任何地方定义response变量。
  • 你可能错过了response = self.client.post("/app.....这一行。
  • 好点,谢谢。我按照你说的做了,现在又犯了一个错误。我会更新我的问题。
  • 你确定 locust 没有得到一个空的响应(出于某种原因)?因为您的新错误消息正是如果它确实会发生的情况......
  • @Saffik 如果您找到了解决方案,请将其作为答案发布并接受。我猜是响应不是json?

标签: python flask locust


【解决方案1】:

解决办法是:

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

output = jsonify(msg=msg)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 2015-02-27
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多