gpt4 book ai didi

go - 虹膜 GO : Get form urlencoded data not working

转载 作者:数据小太阳 更新时间:2023-10-29 03:34:41 27 4
gpt4 key购买 nike

我正在尝试一些非常简单的事情,从我正在构建的应用程序的松弛命令中获取信息,并且由于我是 Go 的新手,我发现 Iris 是一个非常好的框架,所以我决定使用它和它实际工作的网站上的示例 https://iris-go.com/v10/recipe#Read Form90 但是当我从 slack 接收数据时,不是,并且总是出现此错误。

➜  slack-app go run iris.go
[DBUG] 2018/01/19 22:10 POST: /prices -> main.main.func1() and 2 more
[DBUG] 2018/01/19 22:10 Application: running using 1 host(s)
[DBUG] 2018/01/19 22:10 Host: addr is :8080
[DBUG] 2018/01/19 22:10 Host: virtual host is localhost:8080
[DBUG] 2018/01/19 22:10 Host: register startup notifier
[DBUG] 2018/01/19 22:10 Host: register server shutdown on interrupt(CTRL+C/CMD+C)
Now listening on: http://localhost:8080
Application started. Press CMD+C to shut down.
[WARN] 2018/01/19 22:10 Recovered from a route's Handler('main.main.func1')
At Request: 200 /prices POST ::1
Trace: reflect.Value.Interface: cannot return value obtained from unexported field or method

/usr/local/go/src/runtime/asm_amd64.s:509
/usr/local/go/src/runtime/panic.go:491
/usr/local/go/src/reflect/value.go:942
/usr/local/go/src/reflect/value.go:931
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/utils.go:18
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/binder.go:371
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/binder.go:361
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/binder.go:271
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/binder.go:165
/Users/myuser/go/src/github.com/kataras/iris/vendor/github.com/iris-contrib/formBinder/binder.go:153
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:1972
/Users/myuser/go/src/slack-app/iris.go:32
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:900
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:1172
/Users/myuser/go/src/github.com/kataras/iris/middleware/logger/logger.go:50
/Users/myuser/go/src/github.com/kataras/iris/middleware/logger/logger.go:31
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:900
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:1172
/Users/myuser/go/src/github.com/kataras/iris/middleware/recover/recover.go:56
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:913
/Users/myuser/go/src/github.com/kataras/iris/context/context.go:1070
/Users/myuser/go/src/github.com/kataras/iris/core/router/handler.go:219
/Users/myuser/go/src/github.com/kataras/iris/core/router/router.go:70
/Users/myuser/go/src/github.com/kataras/iris/core/router/router.go:147
/usr/local/go/src/net/http/server.go:2619
/usr/local/go/src/net/http/server.go:1801
/usr/local/go/src/runtime/asm_amd64.s:2337

代码

    package main

import (
"github.com/kataras/iris"

"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)

type Student struct {
channel_id string
channel_name string
command string
response_url string
team_domain string
team_id string
text string
token string
trigger_id string
user_id string
user_name string
}

func main() {
app := iris.New()
app.Logger().SetLevel("debug")
app.Use(recover.New())
app.Use(logger.New())

app.Post("/prices", func(ctx iris.Context) {
student := Student{}
err := ctx.ReadForm(&student)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(err.Error())
}

ctx.Writef("Student: %#v", student)
})

app.Run(iris.Addr(":8080"))
}

最佳答案

the documentation 中的示例你链接到建议使用标签/注释将字段映射到表单键,并根据需要保留你的字段名称,即遵循 Go 命名约定并导出:

type Student struct {
ChannelID string `form:"channel_id"`
ChannelName string `form:"channel_name"`
...
}

字段标记在 Go 中很常见,它也在标准 Go 包中的某些地方使用,例如我们,encoding/jsonencoding/xml分别用于 JSON 和 XML 数据的序列化/反序列化。

关于go - 虹膜 GO : Get form urlencoded data not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48352537/

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