如何解决Flask / Connexion应用程序XHR PUT TypeError:缺少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
位于您的路线网址中,例如
@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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。