spin icon indicating copy to clipboard operation
spin copied to clipboard

Rest => put / patch / delete => /path/{id}

Open inaxium opened this issue 2 years ago • 1 comments

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?

inaxium avatar Nov 20 '23 10:11 inaxium

There are a couple of things that can help with this:

  1. At the raw component/trigger level, you can use /path/... and the spin-path-info header. 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/123 then spin-path-info will contain /123 (including the leading / which is not ideal for your use case but should be easy to strip).

  2. You can also route within a Go component using the Router type. 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.

itowlson avatar Nov 20 '23 19:11 itowlson