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

ConstantHTTP method
piko.MethodGetGET
piko.MethodPostPOST (default)
piko.MethodPutPUT
piko.MethodPatchPATCH (partial update)
piko.MethodDeleteDELETE
piko.MethodHeadHEAD
piko.MethodOptionsOPTIONS

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