Skip to content

Latest commit

 

History

History
123 lines (91 loc) · 2.92 KB

requests.md

File metadata and controls

123 lines (91 loc) · 2.92 KB

Requests

Appier maps request URLs to methods using a route decorator on handler methods:

import appier

class HelloApp(appier.App):

    @appier.route("/", "GET")
    def hello(self):
        return "hello from /"

HelloApp().serve()

The previous code will handle the base URL. If you go to http://localhost:8080, you should get a hello message as the response.

You can handle a longer URL path:

@appier.route("/cats/garfield", "GET")
def hello(self):
    return "hello from garfield"

Handling POST requests instead of GET is just as easy:

@appier.route("/cats/garfield", "POST")
def hello(self):
    return "hello from /cats/garfield"

The same handler can also handle both request types:

@appier.route("/cats/garfield", ("GET", "POST"))
def hello(self):
    return "hello from /cats/garfield"

It can also handle multiple URLs:

@appier.route("/cats/garfield", "GET")
@appier.route("/cats/felix", "GET")
def hello(self):
    return "hello from garfield or felix (not sure which)"

Parameters

You can easily capture parts of the URL:

@appier.route("/cats/<name>", "GET")
def hello(self, name):
    return "hello from %s" % name

Those parts can be automatically casted for you by specifying their type:

@appier.route("/numbers/<int:number>", "GET")
def hello(self, number):
    return "this is number %d" % number

If you were to call http://localhost:8080/cats?name=garfield, here's how you would retrieve the name parameter (same goes for form data):

@appier.route("/cats", ("GET", "POST"))
def hello(self):
    name = self.field("name")
    return "hello from %s" % name

You can also specify the default value used when the value is undefined, as well as how to cast the value being retrieved:

@appier.route("/numbers", ("GET", "POST"))
def hello(self):
    number = self.field("number", 5, cast = int)
    return "this is number %d" % number

If you were posting a file using "multipart/form-data" encoding, you could access it like this:

@appier.route("/file", "POST")
def hello(self):
    file = self.field("form_file_name")
    return "you uploaded a file of type %s named %s" % (file.mime, file.name)

Errors

You can return custom responses for specific error codes:

@appier.error_handler(404)
def not_found_code(self, error):
    return "404 - The page you requested was not found"

You can also return custom responses to unhandled exceptions (eg: an object you tried to retrieve was not found):

@appier.exception_handler(appier.NotFoundError)
def not_found(self, error):
    return "The object you requested was not found"

Access control

Routes can be protected so that they can be accessed only by certain authenticated users. To learn more, read the Access Control documentation.