gpt4 book ai didi

go - 使用 Iris-go 处理 body 支柱

转载 作者:IT王子 更新时间:2023-10-29 02:22:50 28 4
gpt4 key购买 nike

我是 Golang 的新手,我一直在做一些代码测试以使用 Iris 框架构建 REST API,我试图从 Post 获取正文数据到我的 API,但我无法让它工作,我读过 body Binder http://iris-go.com/body_binder/并按照示例进行操作。我得到的结果是一个空结构:

我的代码:

package main

import (
"github.com/kataras/iris"
"fmt"
)

type PostAPI struct {
*iris.Context
}

type Lead struct {
fbId string
email string
telefono string
version string
mac string
os string
}

func (p PostAPI) Post(){

lead := Lead{}
err := p.ReadJSON(&lead)

if (err != nil) {

fmt.Println("Error on reading form: " + err.Error())
return
}
fmt.Printf("Post! %v", lead)
}

func main() {

iris.API("/", PostAPI{})
iris.Listen(":8080")
}

帖子:

curl -H "Content-Type: application/json" -X POST -d '{"fbId": "werwer","email": "werwer@gmail.com","telefono": "5555555555","version": "123","mac": "3j:3j:3j:3j","os": "uno bien chido"}' http://0.0.0.0:8080/

结果:

Post! {     }

我做错了什么?

最佳答案

您应该尝试在您的结构中导出带有 json 标签的字段即

 type Lead struct {
FbId string `json:"fbId"`
Email string `json:"email"`
Telefono string `json:"telefono"`
Version string `json:"version"`
Mac string `json:"mac"`
Os string `json:"os"`
}

关于go - 使用 Iris-go 处理 body 支柱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39256975/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com