Date gin/golang gin gonic不解析时间。unix json的时间是否正确?
我无法找到使用UNIX正确执行此代码的方法:Date gin/golang gin gonic不解析时间。unix json的时间是否正确?,date,datetime,go,go-gin,Date,Datetime,Go,Go Gin,我无法找到使用UNIX正确执行此代码的方法: package main import ( "time" "github.com/gin-gonic/gin" "net/http" ) type Things struct { Name string `json:"name"` OneDay time.Time `json:"oneDay"` } type Example struct { Things []Things `json:"things"` Somet
package main
import (
"time"
"github.com/gin-gonic/gin"
"net/http"
)
type Things struct {
Name string `json:"name"`
OneDay time.Time `json:"oneDay"`
}
type Example struct {
Things []Things `json:"things"`
Something int `json:"something"`
}
func TestGinGo(c *gin.Context) {
var example Example
c.BindJSON(&example)
c.JSON(http.StatusOK, gin.H{"data": example})
}
func main() {
r := gin.Default()
r.POST("/", TestGinGo)
r.Run("0.0.0.0:8080")
}
我这样称呼这个端点:
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": "2020-01-01T00:00:00Z"
}],
"something": 2
}'
package main
import (
"time"
"github.com/gin-gonic/gin"
"net/http"
)
type Things struct {
Name string `json:"name"`
OneDay time.Time `json:"oneDay" time_format:"unix"`
}
type Example struct {
Things []Things `json:"things"`
Something int `json:"something"`
}
func TestGinGo(c *gin.Context) {
var example Example
c.BindJSON(&example)
c.JSON(http.StatusOK, gin.H{"data": example})
}
func main() {
r := gin.Default()
r.POST("/", TestGinGo)
r.Run("0.0.0.0:8080")
}
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": 1589898758007
}],
"something": 2
}'
回答是正确的:
{
"data": {
"things": [
{
"name": "bling",
"oneDay": "2020-01-01T00:00:00Z"
}
],
"something": 2
}
}
现在,我稍微更改了代码以使用UNIX,如下所示:
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": "2020-01-01T00:00:00Z"
}],
"something": 2
}'
package main
import (
"time"
"github.com/gin-gonic/gin"
"net/http"
)
type Things struct {
Name string `json:"name"`
OneDay time.Time `json:"oneDay" time_format:"unix"`
}
type Example struct {
Things []Things `json:"things"`
Something int `json:"something"`
}
func TestGinGo(c *gin.Context) {
var example Example
c.BindJSON(&example)
c.JSON(http.StatusOK, gin.H{"data": example})
}
func main() {
r := gin.Default()
r.POST("/", TestGinGo)
r.Run("0.0.0.0:8080")
}
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": 1589898758007
}],
"something": 2
}'
我这样称呼它:
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": "2020-01-01T00:00:00Z"
}],
"something": 2
}'
package main
import (
"time"
"github.com/gin-gonic/gin"
"net/http"
)
type Things struct {
Name string `json:"name"`
OneDay time.Time `json:"oneDay" time_format:"unix"`
}
type Example struct {
Things []Things `json:"things"`
Something int `json:"something"`
}
func TestGinGo(c *gin.Context) {
var example Example
c.BindJSON(&example)
c.JSON(http.StatusOK, gin.H{"data": example})
}
func main() {
r := gin.Default()
r.POST("/", TestGinGo)
r.Run("0.0.0.0:8080")
}
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"things": [{
"name": "bling",
"oneDay": 1589898758007
}],
"something": 2
}'
我现在得到这个错误(400错误格式):
我进了图书馆。。。我看到了使用“unix”的代码:
我真的很想使用unix,因为许多语言不需要库来使用unix,我也不想强制消费者使用特定的格式。。。我看不出我在哪里漏掉了球…@mkopriva,因为您可以看到输入使用的是默认格式RFC3339。。。我只是把这些值作为输出制作一个eco。。。但是当我说format unix时,输入没有被正确解码,我提供了一个数字。。。但这家伙做得很好:我不明白他们是如何或为什么说他们通过使用那个特定的版本来修复它的,你使用的是什么版本的gin?go版本go1.14.2 darwin/AMD64非go版本,gin版本,你的gin版本是什么?在源代码处查看这似乎是一个
表单
只支持输入的功能,不支持json。您需要改用自定义时间类型。@mkopriva,因为您看到输入使用的是默认格式RFC3339。。。我只是把这些值作为输出制作一个eco。。。但是当我说format unix时,输入没有被正确解码,我提供了一个数字。。。但这家伙做得很好:我不明白他们是如何或为什么说他们通过使用那个特定的版本来修复它的,你使用的是什么版本的gin?go版本go1.14.2 darwin/AMD64非go版本,gin版本,你的gin版本是什么?在源代码处查看这似乎是一个表单
只支持输入的功能,不支持json。您将需要改用自定义时间类型。