Rest => put / patch / delete => /path/{id}
Hello,
In REST methods such as PUT, PATCH, and DELETE, it's common to identify the resource to be manipulated using /path/4711.
I've tried the following approaches:
/path/ /path/{id} /path/...
However, none of these attempts yielded the desired result.
In native Go, I would define a path like this: /path/ To capture the value, I would use:
if r.Method == http.MethodPut || r.Method == http.MethodPatch || r.Method == http.MethodDelete {
key := path.Base(path.Clean(r.URL.Path))
}
r is my *http.Request, just for clarification.
Now to my actual question: Has the extraction of an ID from the path been implemented, and if so, what patterns are used for this?
There are a couple of things that can help with this:
-
At the raw component/trigger level, you can use
/path/...and thespin-path-infoheader. See https://developer.fermyon.com/spin/v2/http-trigger#additional-request-information for full details but basically that header contains the stuff that matched the trailing wildcard. So if your trigger route is/path/...and the request is to/path/123thenspin-path-infowill contain/123(including the leading/which is not ideal for your use case but should be easy to strip). -
You can also route within a Go component using the
Routertype. This allows you to match and name individual segments, e.g.router.GET("/path/:id", HandlePath). See https://github.com/fermyon/spin/blob/main/examples/http-tinygo-router/main.go for an example.