Context
I am trying to build a Go Serverless function with vercel, but my route needs a path parameter.
The Problem
How can I map path parameters in golang with vercel?
Attempts
Given the following folder structure
func GetUser(w http.ResponseWriter, r *http.Request) {
usr := strings.TrimPrefix(r.URL.Path, "api/user/")
if cal == "" {
http.Error(w, "Invalid user", http.StatusBadRequest)
return
}
_, err := w.Write([]byte(cal))
if err != nil {
slog.Error("Error answering", err)
}
}
But I when I make a request to /api/user/1 it returns a 404 (not found)
Same but with [id] directory
Same but with [id].go file
system
(system)
January 12, 2025, 2:38am
2
There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.
Sometimes things don’t go as expected when deploying a website. If you see a 404 on your site, that means the page or resource couldn’t be found.
There are many possible causes, and the absence of something can be tricky debug if you don’t know where to look. Here are some things you can do to find the cause and fix it.
Debugging Tips
Check the error code
If you see a mostly white screen with 404: NOT_FOUND along with a Code and and ID then you can click the blue info box below the error deta…
A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0 .
for the moment, I’m limited to query parmeters
anshumanb
(Anshuman Bhardwaj)
January 29, 2025, 12:08pm
4
Hi @gusos-admin , welcome to the Vercel Community!
I think you can create a rewrites from /api/user/(.*)
to api/user.go
file using the vercel.json
. This way you can rewrite all incoming requests to a certain route and handle the path variables.
"rewrites": [{ "source": "api/user/(.*)", "destination": "/api/user.go" }],
I hope this helps.