Flask/Connexion app XHR PUT TypeError: missing 1 required positional argument

Flask/Connexion app XHR PUT TypeError: missing 1 required positional argument

我正在开发 Flask 应用程序,我正在使用 Connexion 配置我的端点。 我的目标是向我的服务器发送一个 PUT 请求,该请求采用 JSON 类型的一个主体参数并将其保存到 JSON 文件中,但是当我发送请求时,我最终遇到内部服务器错误。

我运行陷入的错误:

TypeError: save_config_reqhandler() missing 1 required positional argument: 'config'

我的代码是这样的:

请求

let request = new XMLHttpRequest();
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        if (request.status == 200 || request.status == 420) {
            document.getElementById("saveconfstatus").innerHTML = request.responseText;
        }
    }
};
let url = "/security-testing-tool/config/save";
request.open("PUT", url, true);
request.setRequestHeader("Accept", "text/plain");
request.setRequestHeader("Content-Type", "application/json");
request.send(JSON.stringify(config));

变量 config 是我发送到服务器的 JavaScript 对象。

服务器

@app.route('/security-testing-tool/config/save', methods=['PUT'])
def save_config_reqhandler(config):
    ...

我已经通过单元测试测试了服务器代码,似乎没有问题。

Swagger 配置

/config/save:
    put:
        operationId: server.server.save_config_reqhandler
        tags:
            - Config
        summary: Save a config
        description: Save a config in a json file on the server
        parameters:
            - name: config
              in: body
              description: the name and content of the config
              schema:
                type: object
                additionalProperties: true
        responses:
            200:
                description: Successfully saved config
            420:
                description: Config is not json compatible

Flask 期望 config 在您的路线中 url 例如

@app.route('/security-testing-tool/<config>/save', methods=['PUT']) # config in between <>
def save_config_reqhandler(config):

这似乎不是您想要的。

您似乎想从请求正文中获取配置。

from flask import request
@route('/')
def a_route():
    config = request.json