Flask - how to get HTML form input as Flask-RESTful api data

Viewed 175

I have this form in HTML

<form action="{{ url_for('transfer-api') }}" method="post">
    <p>Sender account id:</p>
    <p><input type="text" name="senderId" id="senderId" minlength="20" maxlength="20"/> * 20 characters</p>
    <p>Receiver account id:</p>
    <p><input type="text" name="receiverId" id="receiverId" minlength="20" maxlength="20"/> * 20 characters</p>
    <p>Amount:</p>
    <p><input type="text" name="amount" id="amount" /> * number</p>
    <input type="submit" value="transfer" />
</form>

I need to send a post request to a flask-restful api I've written

class Transfer(Resource):
    def post(self):
    ...
    return resp

api.add_resource(Transfer, "/api/transfer", endpoint="transfer-api")

If I use the flask-restful api with the requests module, it works fine

response = requests.post(BASE + "/api/transfer", json={"from": "7cdfe1555c4543558887", "to": "f30d031d5f3b49c7b2ca", "amount": 5})
print(response.json())

I want to do the same thing, but with the form input data. My class Transfer doesn't have "request.form["senderId"]" etc, but it gets the args "from", "to" and "amount" using reqparse imported from the flask-restful module. If I run the code as it is, it gives me the message "Did not attempt to load JSON data because the request Content-Type was not 'application/json'." because I'm not giving any data to the api post request.

1 Answers
Related