How to override an action's HTTP method
Actions respond to POST by default. The action protocol reserves a Method() receiver that returns a piko.HTTPMethod, intended to let you switch an action to GET, PUT, PATCH, DELETE, HEAD, or OPTIONS. For the action surface see server actions reference. For the rationale behind the action protocol see about the action protocol.
[!IMPORTANT]The annotator currently registers every action with
Method()as POST regardless of return value. If you need a non-POST endpoint today, use a normal HTTP handler instead. See about the action protocol for why.
Available constants
| Constant | HTTP method |
|---|---|
piko.MethodGet | GET |
piko.MethodPost | POST (default) |
piko.MethodPut | PUT |
piko.MethodPatch | PATCH (partial update) |
piko.MethodDelete | DELETE |
piko.MethodHead | HEAD |
piko.MethodOptions | OPTIONS |
Piko exports the constants and they are safe to reference today, even though the annotator does not yet honour the return value.
Declare an intended method
func (a DeleteAction) Method() piko.HTTPMethod {
return piko.MethodDelete
}
After regeneration the action mounts at /_piko/actions/<package>.<StructName minus "Action"> and accepts only POST. Declaring Method() keeps the source aligned with the planned interface for when codegen begins reading the return value.
Compose with other interfaces
Cacheable, RateLimitable, and ResourceLimitable all compose freely on the same struct. Implement them on the same type to combine behaviours. See How to cache action responses, How to rate-limit an action, and How to set resource limits on an action.
See also
- Server actions reference for the full action surface.
- About the action protocol for the design rationale.
- How to test actions for
NewActionTesterand method-aware assertions.