0

I've trying to locate what is the problem because whatever example I see, none mentions a similar issue when using request args.

This is my code:

@api.route('/clothing/category', methods=['GET'])
def get_clothing_by_category():
    if request.args:
        args = request.args

    if "foo" in args:
        foo = args["foo"]

    if "bar" in args:
        bar = args.get("bar")

    if "baz" in args:
        baz = args["baz"]

    if "title" in request.args:
        title = request.args.get("title")
        serialized = ", ".join(f"{k}: {v}" for k, v in request.args.items())
        return f"(Query) {serialized}", 200
    else:
        return "No query string received", 200 

I've tried declaring global args and doesn't work either. And I really don't know how to solve the problem.

Many thanks!

wjandrea
  • 23,210
  • 7
  • 49
  • 68
  • add a line at the top of the function: `args = {}`, and use `getDefault()` method of dictionary (Faster) – rioV8 Jun 19 '21 at 10:51
  • 1
    Does this answer your question? ["UnboundLocalError: local variable referenced before assignment" after an if statement](https://stackoverflow.com/questions/15367760/unboundlocalerror-local-variable-referenced-before-assignment-after-an-if-sta). TLDR `if not request.args`, `args` is undefined – wjandrea Jun 19 '21 at 16:41
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Jun 19 '21 at 16:44

0 Answers0