The Go SDK is currently in experimental status. If you would like to provide feedback, please reach out to us with your suggestions and comments on our Discord.
Go - Api.Put()
Register an API route and set a specific HTTP PUT handler on that route.
This method is a convenient short version of Api.Route.Put()
import (
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/apis"
)
func main() {
api := nitric.NewApi("public")
api.Put("/hello", func(ctx *apis.Ctx) {
ctx.Response.Body = []byte("Hello World")
})
nitric.Run()
}
Parameters
- Name
path
- 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
handler
- Required
- Required
- Type
- interface{}
- Description
The callback function to handle requests to the given path and method.
- Name
options
- Optional
- Optional
- Type
- ...MethodOption
- Description
Additional options for the route. See below.
Method options
- Name
WithNoMethodSecurity()
- Optional
- Optional
- Type
- MethodOption
- Description
Disables security on the method.
- 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 PUT requests
api.Put("/hello", func(ctx *apis.Ctx) {
ctx.Response.Body = []byte("Hello World")
})
Create a route with path params
api.Put("/hello/:name", func(ctx *apis.Ctx) {
name := ctx.Request.PathParams()["name"]
ctx.Response.Body = []byte("Hello " + name)
})
Access the request body
The PUT request body is accessible using ctx.Request.Data()
.
import (
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/apis"
)
func main() {
api := nitric.NewApi("public")
api.Put("/hello", func(ctx *apis.Ctx) {
data := ctx.Request.Data()
ctx.Response.Body = data
})
nitric.Run()
}