处理Go中的POST请求

huangapple go评论88阅读模式
英文:

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 (
        &quot;net/http&quot;
        &quot;gorilla/schema&quot;
        &quot;fmt&quot;
        &quot;log&quot;
    )
    
    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(&quot;Error parsing form&quot;)
        }
    
        p := new(Payload)
        decoder := schema.NewDecoder()
    
        fmt.Println(r.PostForm)
    
        err = decoder.Decode(p, r.Form)
    
        if err != nil {
            fmt.Println(&quot;Error decoding&quot;)
        }
        fmt.Println(p.slot_temp)
    
    }
    
    func main() {
        mux := http.NewServeMux()
        mux.HandleFunc(&quot;/&quot;, MyHandler)
        log.Fatal(http.ListenAndServe(&quot;:8082&quot;, mux))
    }

I test i with 

    curl -X POST -H &#39;application/x-www-form-urlencoded&#39; -d &quot;slot_temp=34&amp;data=22ltime=1495092909&amp;device=1B3C29&amp;signal=18.63&quot; 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&#39;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 (
&quot;fmt&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;github.com/gorilla/schema&quot;
)
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(&quot;Error parsing form&quot;)
}
p := new(Payload)
decoder := schema.NewDecoder()
fmt.Println(r.PostForm)
err = decoder.Decode(p, r.Form)
if err != nil {
fmt.Println(&quot;Error decoding&quot;)
}
fmt.Println(p.Slot_temp)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc(&quot;/&quot;, MyHandler)
log.Fatal(http.ListenAndServe(&quot;:8082&quot;, mux))
}

huangapple
  • 本文由 发表于 2017年5月18日 19:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/44047147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定