问题描述
我正在开发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
位于您的路线网址中,例如
@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