Go - Api.Delete()

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

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  api, err := nitric.NewApi("public")
  if err != nil {
    return
  }

  api.Delete("/hello", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    ctx.Response.Body = []byte("Hello World")
    return next(ctx)
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

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. See create a route with path params.

  • 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 handler.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.Delete("/hello", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
  ctx.Response.Body = []byte("Hello World")
  return next(ctx)
})

Create a route with path params

api.Delete("/hello/:name", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.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/handler"
	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	defaultOidcRule := nitric.OidcRule("user", "https://example-issuer.com", []string{"YOUR-AUDIENCES"})

	secureApi, err := nitric.NewApi(
		"secure",
		nitric.WithSecurity(defaultOidcRule([]string{"products:read"})))
	if err != nil {
		return
	}

	// Override the APIs security requirements with `WithNoMethodSecurity()`
	secureApi.Delete("/public", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
		// Handle request
		return next(ctx)
	}, nitric.WithNoMethodSecurity())

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

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/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func authMiddleware(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
  // Perform auth validation
  return next(ctx)
}

func handleRequest(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.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.Delete("/hello/:name", handler.ComposeHttpMiddleware(authMiddleware, handleRequest))

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Access the request body

The DELETE request body is accessible using ctx.Request.Data().

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  api, err := nitric.NewApi("public")
  if err != nil {
    return
  }

  api.Delete("/hello", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
    data := ctx.Request.Data()

    ctx.Response.Body = data

    return next(ctx)
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}