Go - Api.Route.Delete()
Register a handler for HTTP DELETE requests to the route.
import ("fmt""github.com/nitrictech/go-sdk/faas""github.com/nitrictech/go-sdk/nitric")func main() {api, err := nitric.NewApi("public")if err != nil {return}api.NewRoute("/hello").Delete(func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {ctx.Response.Body = []byte("Hello World")return next(ctx)})nitric.Run()}
Parameters
- Name
middleware
- Required
- Required
- Type
- HttpMiddleware
- Description
The middleware function to use as the handler for HTTP requests. If you want to compose more than one middleware use
faas.ComposeHttpMiddleware
.
- Name
options
- Optional
- Optional
- Type
- ...MethodOption
- Description
Additional options for the route. See below.
Method options
- Name
WithNoMethodSecurity()
- Optional
- Optional
- Type
- MethodOption
- Description
Sets a base path for all the routes in the API.
- Name
WithMethodSecurity()
- Optional
- Optional
- Type
- MethodOption
- Description
Overrides a security rule from API defined JWT rules.
- Name
name
- Required
- Required
- Type
- string
- Description
The name of the security rule.
- Name
scopes
- Required
- Required
- Type
- []string
- Description
The scopes of the security rule.
Examples
Register a handler for DELETE requests
api.NewRoute("/hello").Delete(func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {ctx.Response.Body = []byte("Hello World")return next(ctx)})
Create a route with path params
api.NewRoute("/hello/:name").Delete(func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {name := ctx.Request.PathParams()["name"]ctx.Response.Body = []byte("Hello " + name)return next(ctx)})
Create a route with no method security
import ("fmt""github.com/nitrictech/go-sdk/nitric")func main() {secureApi, err := nitric.NewApi("secure",nitric.WithSecurityJwtRule("user", nitric.JwtSecurityRule{Issuer: "https://example-issuer.com",Audiences: []string{ "YOUR-AUDIENCES" },}),nitric.WithSecurity("user", []string{"products:read"}),)if err != nil {return}// Override the APIs security requirements with `WithNoMethodSecurity()`api.NewRoute("/public").Delete(func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {// Handle requestreturn next(ctx)}, nitric.WithNoMethodSecurity())nitric.Run()}
Chain functions as a single method handler
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
import ("fmt""github.com/nitrictech/go-sdk/faas""github.com/nitrictech/go-sdk/nitric")func authMiddleware(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {// Perform auth validationreturn next(ctx)}func handleRequest(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {name := ctx.Request.PathParams()["name"]ctx.Response.Body = []byte("Hello " + name)return next(ctx)}func main() {api, err := nitric.NewApi("private")if err != nil {return}api.NewRoute("/hello/:name").Delete(faas.ComposeHttpMiddleware(authMiddleware, handleRequest))nitric.Run()}
Access the request body
The DELETE request body is accessible using ctx.Request.Data()
.
import ("fmt""github.com/nitrictech/go-sdk/faas""github.com/nitrictech/go-sdk/nitric")func main() {api, err := nitric.NewApi("public")if err != nil {return}api.NewRoute("/hello").Delete(func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {data := ctx.Request.Data()ctx.Response.Body = datareturn next(ctx)})nitric.Run()}
Last updated on Dec 19, 2024