Get Client-Side InformationΒΆ

We have talked Routing on the last chapter, but how to get information from the client side?

Get and post form are the most common ways for server to get information from the server. For a get form, all arugments go with the link, and for a post form, all arguments will be the body of the request.

After FutureFinity receives a request, FutureFinity will parse the request, at that time, the form will be parsed too.

All the arguments that come from the link can be got by using RequestHandler.get_link_arg classmethod. And the arugments that come from the body can be got by using RequestHandler.get_body_arg classmethod.

Let’s see the example below:

import futurefinity.web

import asyncio

app = futurefinity.web.Application()


@app.add_handler("/")
class MainHandler(futurefinity.web.RequestHandler):
    async def get(self, *args, **kwargs):
        username = self.get_link_arg("username", default=None)
        if self.get_link_arg("username"):
            return "Hi, %s!" % username

        return ("<form method=\"post\">"
                "<input type=\"text\" name=\"username\">"
                "<input type=\"submit\" value=\"submit\">"
                "</form>")

    async def post(self, *args, **kwargs):
        username = self.get_body_arg("username")
        return "Hi, %s!" % username


app.listen(23333)

try:
    asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
    pass

The example will try to get the username from the link first, if it fails, then it will show you a form for you to fill and submit by post.