我是Go和Gin的新手,我在打印完整的请求正文时遇到了麻烦.
我希望能够从第三方POST读取请求正文,但我得到空请求正文
curl -u dumbuser:dumbuserpassword -H "Content-Type: application/json" -X POST --data '{"events": "3"}' http://localhost:8080/events
Run Code Online (Sandbox Code Playgroud)
我的整个代码如下.任何指针都很赞赏!
package main
import (
"net/http"
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
authorized := router.Group("/", gin.BasicAuth(gin.Accounts{
"dumbuser": "dumbuserpassword",
}))
authorized.POST("/events", events)
router.Run(":8080")
}
func events(c *gin.Context) {
fmt.Printf("%s", c.Request.Body)
c.JSON(http.StatusOK, c)
}
Run Code Online (Sandbox Code Playgroud) 我是Go的新手,但到目前为止,我非常喜欢它。
我有一个我不知道的问题。我正在将API从Node迁移到Go,并且有此日志必须捕获POST 原样并将其保存到jsonbPostgresql数据库的type列中。
这意味着我不能使用struct或预定的任何东西。
POST是使用原始身体制作的,Content-Type: application/json如下所示:
{
"debug": false,
"order_id_gea": 326064,
"increment_id_gea": 200436102,
"date": "2017-05-18T01:44:44+00:00",
"total_amount": 10000.00,
"currency": "MXN",
"payment_method": "Referencia bancaria",
"reference": "857374",
"buyer": {
"buyer_id_gea": 1234,
"full_name": "Juan Perez Martinez",
"email": "juanperez@gmail.com",
"phone": "5512341234"
},
"products": [
{
"sku":"PEP16114",
"price": 10000.00,
"currency": "MXN",
"student": {
"school_id_gea": 172,
"grade_id_gea": 119,
"level_id_gea": 36,
"name": "Benancio",
"last_name": "Perez",
"second_last_name": "Garcia",
"email": "benancio@gmail.com"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
在Node + Hapi上非常简单:
const payload = request.payload
Run Code Online (Sandbox Code Playgroud)
然后我可以从访问JSON …