Comments (7)
🤔 looks like the endpoint route definition its pretty weird,
maybe if you try something like this based in the documentation, obviously if you write the de defition of the
url.
try passing from
r.GET("/odata/Resource(:id)", QuerySingleResource)
to this :
r.GET("/odata/Resource/:id", QuerySingleResource)
from gin.
@RodEspinoza Thats not really possible. The ODATA standard specifies it should be /Resource(:id)
and the tests that I have to run against the API for certification require it that way.
from gin.
I figured it out and it is an issue it seems.
In order to pull the parameter when you do this you have to use
c.Param("id)")
For some reason it includes the ) as part of the parameter name, as well it also adds ) to the end of the value of the parameter...
from gin.
The ODATA protocol may look a little strange.
The following code is a general practice.
r.GET("/odata/Resource:id", func(c *gin.Context) {
param := c.Param("id")
c.String(http.StatusOK, param)
})
curl 'http://127.0.0.1:8080/odata/Resource2' >>> 2
curl 'http://127.0.0.1:8080/odata/Resource2/23' >>> not found
curl 'http://127.0.0.1:8080/odata/Resource2?abc' >>> 2
if you want to achieve the results you want to see the answer below
from gin.
🤔 based in odata, maybe you can try with regex to extract the param :
if this works maybe you can try to generate a pull requests with the function 👀
this maybe could help :
package main
import (
"fmt"
"regexp"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/odata/Resource(:id)", QuerySingleResource)
r.Run(":8080")
}
// maybe you can generate a middleware to extract params based in odata format, that return a map or somethign required
func QuerySingleResource(c *gin.Context) {
// Get the part of the URL that contains the :id parameter
urlPart := c.Param("id")
// Use regular expressions to extract the value between parentheses
re := regexp.MustCompile(`\((.*?)\)`)
matches := re.FindStringSubmatch(urlPart)
if len(matches) < 2 {
c.JSON(400, gin.H{"error": "No valid value found for :id"})
return
}
// The extracted value is in matches[1]
id := matches[1]
c.JSON(200, gin.H{"id": id})
}
from gin.
🤔 基于 odata,也许你可以尝试使用正则表达式来提取参数: 如果这有效,也许你可以尝试使用函数 👀 生成拉取请求
这也许会有所帮助:
包主
import ( "fmt" "regexp" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/odata/Resource(:id)", QuerySingleResource) r.Run(":8080") } // maybe you can generate a middleware to extract params based in odata format, that return a map or somethign required func QuerySingleResource(c *gin.Context) { // Get the part of the URL that contains the :id parameter urlPart := c.Param("id") // Use regular expressions to extract the value between parentheses re := regexp.MustCompile(`\((.*?)\)`) matches := re.FindStringSubmatch(urlPart) if len(matches) < 2 { c.JSON(400, gin.H{"error": "No valid value found for :id"}) return } // The extracted value is in matches[1] id := matches[1] c.JSON(200, gin.H{"id": id}) }
Hi,urlPart := c.Param("id") in your code does not match to get data
@alifemove Can try
r.GET("/odata/Resource:(id)", func(c *gin.Context) {
param := c.Param("(id)")
ID := param[1:len(param)-1]
c.String(http.StatusOK, ID)
})
from gin.
@RedCrazyGhost Yeah I think that would be the easiest approach given the circumstance.
I think the underlying issue is using : to signify variables and allowing any valid url character as part of the variable even though you can't name a variable with () in code. Using { } like mux and other routers do is the safer bet since the variable has a definitive beginning and end.
from gin.
Related Issues (20)
- Is there any way to change the http request method? HOT 2
- Golang-Gin HOT 4
- How to use “Accepted” HOT 1
- Trailers sent twice if no body written HOT 2
- Pass variable in rout HOT 1
- Register tauri:// scheme (or allow custom schemes upstream?)
- please support get parms from Transfer-Encoding:compress-deflate
- unexpected EOF during install HOT 1
- sse panic on fasthttphandler HOT 1
- Will it be made into a more comprehensive framework later on? HOT 4
- How does the gin close the log file? HOT 3
- Should we use url.PathUnescape instead of url.QueryUnescape when UnescapePathValues is true
- go get failed: The requested name is valid, but no data of the requested type was found HOT 1
- Attach Response Headers (or middlewares) to redirected requests (CORS issues) HOT 1
- Should context.GetRawData() also copy body to BodyBytesKey? HOT 3
- YAML BindBody should not parse JSON content HOT 1
- Gin do not support javascript ? HOT 5
- Does gin.Recovery() run before or after Group middleware? HOT 1
- How can i prevent a http connection to close HOT 3
- BasicAuth is not suitable for proxy auth
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.