Python - api.methods()
Register an API route that handles a set of specific HTTP verbs.
from nitric.resources import apifrom nitric.application import Nitricfrom nitric.context import HttpMethodpublicApi = api("public")@publicApi.methods([HttpMethod.PUT, HttpMethod.PATCH] "/customer/:customerId")async def customer_update(ctx):id = ctx.req.params.customerIdctx.res.body = f"Updating customer {id}"Nitric.run()
Parameters
- Name
methods
- Required
- Required
- Type
- List[HttpMethod]
- Description
The methods that this route will handle.
- 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. See examples.
- Name
options
- Required
- Required
- Type
- MethodOptions
- Description
Options to configure the route such as security options.
Examples
Register a handler for PUT and PATCH requests
from nitric.resources import apifrom nitric.application import NitricpublicApi = api("public")@publicApi.methods([HttpMethod.PUT, HttpMethod.PATCH] "/customer/:customerId")async def hello_world(ctx):id = ctx.req.params.customerIdctx.res.body = f"Updating customer {id}"Nitric.run()
Last updated on Nov 14, 2024