Python - api.route.post()

Register an API route and set a specific HTTP POST handler on that route.

from nitric.resources import api
from nitric.application import Nitric
publicApi = api("public")
@publicApi.post("/customer")
async def customer_create(ctx):
ctx.res.body = f"Creating new customer"
Nitric.run()

Parameters

  • Name
    match
    Required
    Required
    Type
    string
    Description

    The path matcher to use for the route. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers.

  • Name
    options
    Required
    Required
    Type
    MethodOptions
    Description

    Options to configure the route such as security options.

Examples

Register a handler for POST requests

from nitric.resources import api
from nitric.application import Nitric
publicApi = api("public")
@publicApi.post("/customer")
async def hello_world(ctx):
ctx.res.body = f"Creating new customer"
Nitric.run()

Get request body as JSON

from nitric.resources import api
from nitric.application import Nitric
publicApi = api("public")
@publicApi.post("/customer")
async def hello_world(ctx):
# returns None if request body is not JSON
body = ctx.req.json
ctx.res.body = f"Creating new customer"
Nitric.run()
Last updated on Oct 14, 2024