Giter VIP home page Giter VIP logo

Comments (7)

RodEspinoza avatar RodEspinoza commented on April 27, 2024 1

🤔 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.

alifemove avatar alifemove commented on April 27, 2024 1

@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.

alifemove avatar alifemove commented on April 27, 2024

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.

RedCrazyGhost avatar RedCrazyGhost commented on April 27, 2024

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.

RodEspinoza avatar RodEspinoza commented on April 27, 2024

🤔 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.

RedCrazyGhost avatar RedCrazyGhost commented on April 27, 2024

🤔 基于 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.

alifemove avatar alifemove commented on April 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.