Search code examples
flaskxmlhttprequestputinternal-server-errorconnexion

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


I am working on a Flask App and I am using Connexion to configure my endpoints. My goal is to send a PUT request to my server which takes one body parameter of type JSON and saves that into a JSON file but when I send the request I end up with an internal server error.

The error that I am running into:

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

My code looks like that:

Request

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

The variable config is the JavaScript Object that I am sending to the server.

Server

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

I've already tested the server code via unit tests and there doesn't seem to be a problem.

Swagger Config

/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

Solution

  • Flask is expecting config to be in your route url e.g.

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

    This doesn't appear to be what you want.

    It looks like you want to get config from your request body.

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