'First time using Flask: TypeError: _set_response() missing 1 required positional argument: 'self'

This is my first time using Flask so I apologize if this is a simple mistake on my end. I'm trying to create a mock payment processing system that passes form data to Square Checkout API. When running the project I am receiving the following TypeError:

TypeError: _set_response() missing 1 required positional argument: 'self'

Looking up other questions on this subject I saw that I need to instantiate 'self'. How do I do this using Flask?

Here is my code:

from flask import Flask, redirect, url_for, render_template, request
from pprint import pprint
from square.client import Client
from dotenv import load_dotenv
load_dotenv()
import logging
import urllib.parse
import urllib.request
import os
import uuid

app = Flask(__name__)

client = Client(
    access_token = os.getenv("SQUARE_TOKEN"),
    environment = "sandbox"
)

@app.route("/", methods=["POST", "GET"])
def payment():
    return render_template("payment.html")

@app.route("/make-payment", methods=["POST", "GET"])
def _set_response(self):
    self.send_response(200)
    self.end_headers()

def makePayment(self):
    content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
    post_data = self.rfile.read(content_length) # <--- Gets the data itself
    logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
                str(self.path), str(self.headers), post_data.decode('utf-8'))

    #Query and format Python object
    form_query = dict(urllib.parse.parse_qsl(post_data.decode('utf-8')))

    item_name = str(form_query['name'])
    purchase_amount = float(form_query['amount'])
    converted_amount = int(purchase_amount*100)

    result = client.checkout.create_checkout(
        location_id = f"{os.getenv('LOCATION_ID')}",
        body = {
            "idempotency_key": f"{uuid.uuid4()}",
            "order": {
                "order": {
                    "location_id": f"{os.getenv('LOCATION_ID')}",
                    "line_items": [
                        {
                            "name": f"{item_name}",
                            "quantity": "1",
                            "base_price_money": {
                                "amount": converted_amount,
                                "currency": "USD"
                            }
                        }
                    ]
                }
            }
        }
    )

    if result.is_success():
        pprint(result.body)
    elif result.is_error():
        print(result.errors)

    _set_response()


if __name__ == "__main__":
    app.run(host="localhost", port=8000, debug=True)

Traceback:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
TypeError: _set_response() missing 1 required positional argument: 'self'


Solution 1:[1]

The input file is not UTF-8, it is likely some other code page.

Determine what the correct encoding is and alter your program accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jim Garrison