英文:
Handling POST request in go
问题
我遇到了解码gorilla/schema的POST请求的问题。
我的代码创建了一个基本的HTTP服务器:
package main
import (
"net/http"
"github.com/gorilla/schema"
"fmt"
"log"
)
type Payload struct{
slot_temp string
data string
time string
device string
signal string
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Println("解析表单时发生错误")
}
p := new(Payload)
decoder := schema.NewDecoder()
fmt.Println(r.PostForm)
err = decoder.Decode(p, r.Form)
if err != nil {
fmt.Println("解码时发生错误")
}
fmt.Println(p.slot_temp)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", MyHandler)
log.Fatal(http.ListenAndServe(":8082", mux))
}
我使用以下命令进行测试:
curl -X POST -H 'application/x-www-form-urlencoded' -d "slot_temp=34&data=22&time=1495092909&device=1B3C29&signal=18.63" localhost:8082
但是我得到了以下输出:
map[device:[1B3C29] signal:[18.63] slot_temp:[34] data:[22] time:[1495092909]]
// map的内容和一个空行,而不是p.slot_temp的值
结构字段的内容从未被打印出来。
我猜测解码器存在问题,但我无法找出问题所在。
谢谢。
<details>
<summary>英文:</summary>
I am having trouble decoding POST request with gorilla/schema.
My code is creating a basic http server:
package main
import (
"net/http"
"gorilla/schema"
"fmt"
"log"
)
type Payload struct{
slot_temp string
data string
time string
device string
signal string
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Println("Error parsing form")
}
p := new(Payload)
decoder := schema.NewDecoder()
fmt.Println(r.PostForm)
err = decoder.Decode(p, r.Form)
if err != nil {
fmt.Println("Error decoding")
}
fmt.Println(p.slot_temp)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", MyHandler)
log.Fatal(http.ListenAndServe(":8082", mux))
}
I test i with
curl -X POST -H 'application/x-www-form-urlencoded' -d "slot_temp=34&data=22ltime=1495092909&device=1B3C29&signal=18.63" localhost:8082
But I get the following output
map[device:[1B3C29] signal:[18.63] slot_temp:[34] data:[22] time:[1495092909]]
// content of map and a blank line instead if the value of p.slot_temp
The content of the structure field is never printed.
I guess there is an issue with the decoder, but I can't figure out what is wrong.
Thanks
</details>
# 答案1
**得分**: 0
将结构体字段的名称更改为大写。您的解码器是外部包,无法访问您的结构体。
```go
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/schema"
)
type Payload struct {
Slot_temp string
Data string
Time string
Device string
Signal string
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Println("Error parsing form")
}
p := new(Payload)
decoder := schema.NewDecoder()
fmt.Println(r.PostForm)
err = decoder.Decode(p, r.Form)
if err != nil {
fmt.Println("Error decoding")
}
fmt.Println(p.Slot_temp)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", MyHandler)
log.Fatal(http.ListenAndServe(":8082", mux))
}
请注意,我只翻译了您提供的代码部分,不包括注释和打印语句。
英文:
Change your struct fields name to uppercase. Your decoder is external package and it doesn't have access to your struct
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/schema"
)
type Payload struct {
Slot_temp string
Data string
Time string
Device string
Signal string
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Println("Error parsing form")
}
p := new(Payload)
decoder := schema.NewDecoder()
fmt.Println(r.PostForm)
err = decoder.Decode(p, r.Form)
if err != nil {
fmt.Println("Error decoding")
}
fmt.Println(p.Slot_temp)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", MyHandler)
log.Fatal(http.ListenAndServe(":8082", mux))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论