GET() missing 1 required positional argument : how do i format a GET request to include parameters?

1.8k Views Asked by At

I have a webapp with the following code, which is supposed to serve up any .html file I have stored in the htmls/ directory, provided the two parameters urlhash and predictiontag are correct.

import web

urls = (
    '/htmlview/(.*)', 'htmlview'
)

class htmlview:
    def GET(self, urlhash, predictiontag):
        cache_dir = os.path.join("htmls\\", urlhash)
        htmlpath = os.path.join(cache_dir, predictiontag + ".html")
        with open(htmlpath, "r", encoding="utf-8") as f:
            data = f.read()
        return data

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

I don't know how to format the GET request from a browser window in order to actually access these files. I tried http://localhost:8080/htmlview/a?urlhash=6355865449554195623&predictiontag=Primary but it gave me the error:

<class 'TypeError'> at /htmlview/a
GET() missing 1 required positional argument: 'predictiontag'

For reference, here is the other post I was following: How to serve file in webpy?

3

There are 3 best solutions below

0
Travis On

I use flask but I am guessing that webpy wants you to specify multiple groups in the url matching pattern. (.*) probably matches the whole query string as a single argument to the GET function. Maybe you can use something like urls = ('/htmlview/(.*)&(.*)', 'htmlview')

Again, just guessing.

0
wheeeee On

After some trial and error, here is what worked for me:

urls = (
    '/htmlview/(.*)/(.*)', 'htmlview'
)

class htmlview:
    def GET(self, urlhash, predictiontag):
        cache_dir = os.path.join("htmls\\", urlhash)
        htmlpath = os.path.join(cache_dir, predictiontag + ".html")
        with open(htmlpath, "r", encoding="utf-8") as f:
            data = f.read()
        return data

It seems web.py automatically takes the first /(.*) to be the first parameter and the second to be the second parameter.

0
pbuck On

Use urls regular expression to match url path.

Use web.input() to access query string.

For your example url: http://localhost:8080/htmlview/a?urlhash=6355865449554195623&predictiontag=Primary

url = ( '/htmlview/(.*)', 'htmlview', )

Will pass only 'a' as the first parameter to htmlview.GET(). The remainder is obtainable within htmlview.GET() using web.input():

class htmlview:
    def GET(self, path):
        urlhash = web.input().urlhash
        predictiontag = web.input().predictiontag