如何打印蝗虫响应(JSONDecodeError)

时间:2020-04-13 19:08:57

标签: python flask locust

我有一个烧瓶应用程序,看起来像下面这样: (请注意,出于这个问题,我已对其进行了简化)

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

任何想法我如何打印flask应用程序返回的“ msg”

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

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

测试消息示例

1 个答案:

答案 0 :(得分:2)

解决方案是:

返回的响应不是json,最终返回为JSON时,它可以工作。 此行缺失:

output = jsonify(msg=msg)