Description
I'm sending a GET request containing a date with a timezone but when I call c.Query("mydate")
, I can see that +02:00
is being replaced by 02:00
println(c.Request.URL.RawQuery)
// mydate=2023-04-01T13:00:00+02:00
println(c.Query("mydate"))
// 2023-04-01T13:00:00 02:00
date, err = time.Parse(time.RFC3339, from)
if err != nil {
c.JSON(http.StatusBadRequest, "bad date param- "+err.Error())
return
}
How to reproduce
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// call /mydate=2023-04-01T13:00:00+02:00
// if using vscode REST client, then you need to encode + like below:
// /mydate=2023-04-01T13:00:00%2B02:00
func main() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
println(c.Request.URL.RawQuery)
println(c.Query("mydate"))
date, err := time.Parse(time.RFC3339, c.Query("mydate"))
if err != nil {
c.JSON(http.StatusBadRequest, "bad date param- "+err.Error())
return
}
c.String(200, date.String())
})
err := g.Run("localhost:7000")
if err != nil {
log.Fatalln(err.Error())
}
}
Expectations
$ curl "http://localhost:7000?mydate=2023-04-01T13:00:00%2B02:00"
2023-04-01T13:00:00%2B02:00
Actual result
$ curl "http://localhost:9000?mydate=2023-04-01T13:00:00%2B02:00"
bad from date parameter - parsing time "2023-04-01T13:00:00 02:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"
Environment
- go version: 1.19
- gin version (or commit ref): v1.8.1
- operating system: macOS Monterey 12.6.1
Comment From: mstmdev
go println(c.Request.URL.RawQuery) // mydate=2023-04-01T13:00:00+02:00 println(c.Query("mydate")) // 2023-04-01T13:00:00 02:00
It seems to not encode the '+', please try again.
Comment From: BigBoulard
@mstmdev I'm very sorry man, I spoke too soon. I just figure out that my date was sent by another service that didn't percent-encode the URL using url.QueryEscape(mydate.Format(time.RFC3339))
. You're dead right, there's no issue here, you can close this one. Thank you so much.