Iris GO:获取表单 urlencoded 数据不起作用
Iris GO: Get form urlencoded data not working
我正在尝试一些非常简单的事情,从我正在构建的应用程序的松弛命令中获取信息,并且由于我是 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"))
}
我也是 Go 的新手,但错误似乎表明它正在尝试读取未导出的字段 - 我看到您的 Student
结构具有所有未导出的字段(Go 处理大写字段as exported and uncapitalized fields 作为相反)
您链接到的 the documentation 中的示例建议使用 tag/annotation 将字段映射到表单键,并根据需要保留您的字段名称,即Go 命名约定并导出 :
type Student struct {
ChannelID string `form:"channel_id"`
ChannelName string `form:"channel_name"`
...
}
字段标签在 Go 中很常见,它也在标准 Go 包中的某些地方使用,例如我们,encoding/json
and encoding/xml
for serialization/deserialization of JSON and XML数据,分别。
我正在尝试一些非常简单的事情,从我正在构建的应用程序的松弛命令中获取信息,并且由于我是 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"))
}
我也是 Go 的新手,但错误似乎表明它正在尝试读取未导出的字段 - 我看到您的 Student
结构具有所有未导出的字段(Go 处理大写字段as exported and uncapitalized fields 作为相反)
您链接到的 the documentation 中的示例建议使用 tag/annotation 将字段映射到表单键,并根据需要保留您的字段名称,即Go 命名约定并导出 :
type Student struct {
ChannelID string `form:"channel_id"`
ChannelName string `form:"channel_name"`
...
}
字段标签在 Go 中很常见,它也在标准 Go 包中的某些地方使用,例如我们,encoding/json
and encoding/xml
for serialization/deserialization of JSON and XML数据,分别。