Flask/Connection应用程序XHR PUT类型错误:缺少1个必需的位置参数



我正在开发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

最新更新