HTTP

The DETA library provides a small Flask-inspired http router. For a more complete http router, consider using an external framework in your DETA program such as starlette, FastAPI, Flask, or Django.

app.lib.http(route, methods=None) takes two arguments which specify the decorated function to execute following an HTTP request.

Note

From DETA lib version 21 the convention is app.lib.http. For all earlier lib versions the convention is app.http. You can find a program's lib version by the deta.lib field in the INFO tab. The latest lib version is 22.

Arguments

  • route: str which specifies the path for a corresponding HTTP request. Examples: "/", "/hello".
    • This argument is required.
  • methods: list of HTTP methods which can trigger the function.
    • Defaults to all the standard HTTP methods.
    • Example: ["GET", "PUT"].

Responses

Refer to Responses docs.


Usage Example

from deta.lib import app

@app.lib.http("/", methods=["GET"])
def get_handler(event):
    name = event.params.get("name")
    return {"greeting": f"Hallo, {name}!"}

@app.lib.http("/kiez/", methods=["POST"])
def post_handler(event):
    kiez = event.json.get("kiez")
    return f"Your favorite Kiez in Berlin is {kiez}."


Request-Response Pairs

Request

GET app.deta.sh/<prog_path>/?name=Beverly

Response

{"greeting": "Hallo, Beverly!"}


Request

POST app.deta.sh/<prog_path>/kiez/

with payload:

{
    "kiez": "Akazienkiez"
}

Response

"Your favorite kiez in Berlin is Akazienkiez."


event attributes

  • event.type : str instantiated to "http"
  • event.method: str the HTTP method.
  • event.path: str the HTTP path.
  • event.headers: dict the HTTP headers.
  • event.params: dict the HTTP query string parameters.
  • event.content_type: str the value from the Content-Type header.
  • event.isb64: bool True if body is base64 encoded.
  • event.json : dict provides the json payload as a Python dict.
  • event.cookies : dict the HTTP cookies.