Hi! May be wrong place to ask, sorry. I need to handle dynamically named files from the root of my site for testing purpose. I.e.: /test_12335664.txt /test_98765544.txt Files have dynamic part in name. How I can do this? Route "*" fails with error: panic: wildcards must be named with a non-empty name in path '/' Same error for "\test": panic: wildcards must be named with a non-empty name in path '/test*'
Comment From: thinkerou
router.GET("/test/*id", func(c *gin.Context) {
id := c.Param("id")
// use id to handle your function
// ....
}
right? or you can walk your directory.
Comment From: parserpro
No. "test" is a part of the file name, not a directory. And I can't walk - it used for automated testing.
Comment From: thinkerou
please post some your example code, I think your question is't question of gin.
Comment From: parserpro
You can find it here: https://gist.github.com/parserpro/8a464ad2d84de88e96609f9093407674
Comment From: thinkerou
@parserpro sorry, gin not support the router r.GET("/sputnik*/", handleTest)
, as above point, you may use r.GET("/api/*sputnik", func(c *gin.Context) {}
and get sputnik
value which deal with it.
Comment From: parserpro
Found NoRoute handler - using it to emulate what I need. Really annoying than it's impossible to handle files in the root folder of the site...
Comment From: thinkerou
Sure, use NoRoute
.
Comment From: HunterHeston
Found this thread going trying learn gin and setup a wildcard on the root dir.
I believe the correct answer to:
r.GET("/:someparam", func(c *gin.Context) { /* code goes here */ })
This will match things like example.com/a
, example.com/b
, example.com/asdf
. And provide the a
, b
or asdf
values as a param accessible by:
value := c.Param("someparam")
Anyways that's what I was looking for. Hope this helps someone.
Comment From: bnbabu55
Found this thread going trying learn gin and setup a wildcard on the root dir.
I believe the correct answer to:
go r.GET("/:someparam", func(c *gin.Context) { /* code goes here */ })
This will match things like
example.com/a
,example.com/b
,example.com/asdf
. And provide thea
,b
orasdf
values as a param accessible by:
go value := c.Param("someparam")
Anyways that's what I was looking for. Hope this helps someone.
I am building a proxy server where I will be receiving requests from some enterprise applications with {{baseURL}}/{{end points}}, in this situation, applying your logic did not solve my problem, so I asked ChatGPT to help me out. Here is what it suggested, and it worked for me.
I had to do like below because I have a /ping, /token, and other routes defined already. So, putting any wildcard on the "/" route was a problem, gin was panicking, so I just put a separate variable "param1" for the first param and then the wildcard takes the rest.
authRoutes.GET("/:param1/*wildcard", server.Home)
authRoutes.POST("/:param1/*wildcard", server.Home)
authRoutes.PUT("/:param1/*wildcard", server.Home)
authRoutes.PATCH("/:param1/*wildcard", server.Home)
authRoutes.DELETE("/:param1/*wildcard", server.Home)
You can receive ctx.Param("param1") and ctx.Param("wildcard") in your Home handler, then you should be able to put them back together for a complete path and use it as you, please.
I may get any routes with any methods, so I had to define all the appropriate methods calling the same handler function. Then my handler will call a 3rd-party API with the method, {{3rd Party base URL}}/{{path received}} and the body received.
Hope this helps someone in the future.