Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将数据过帐到端点后请求正文为空_Go_Gorilla - Fatal编程技术网

Go 将数据过帐到端点后请求正文为空

Go 将数据过帐到端点后请求正文为空,go,gorilla,Go,Gorilla,我不确定在运行以下curl请求时,为什么要发布的数据不存在: curl --request POST http://localhost:4000 --header "Content-Type: application/json" --data '{ "hostname": "bbc.co.uk" }' 根据下面的代码。它本质上只是用变量hostname发布json,但由于某种原因,它没有出现在req.Body或域结构数组中。请注意,这是基于 JSON编解码器忽略hostname字段,因为该字

我不确定在运行以下curl请求时,为什么要发布的数据不存在:

curl --request POST http://localhost:4000 --header "Content-Type: application/json" --data '{ "hostname": "bbc.co.uk" }'
根据下面的代码。它本质上只是用变量
hostname
发布json,但由于某种原因,它没有出现在
req.Body
结构数组中。请注意,这是基于

  • JSON编解码器忽略
    hostname
    字段,因为该字段不是。通过将字段名称大写来修复
  • 域上存在数据竞争。通过使用保护对变量的访问来修复
    
  • 应用程序将忽略错误。通过检查并处理JSON解码器返回的错误进行修复
以下是修复代码:

package main

import (
    "encoding/json"
    "log"
    "net/http"
    "sync"

    "github.com/gorilla/handlers"
    "github.com/gorilla/mux"
)

type Domain struct {
    Hostname string `json:"hostname,omitempty"`
}

var (
    domains []Domain
    mu      sync.Mutex
)

func CreateDomainEndpoint(w http.ResponseWriter, req *http.Request) {
    var domain Domain
    if err := json.NewDecoder(req.Body).Decode(&domain); err != nil {
        http.Error(w, "bad request", 400)
        return
    }
    mu.Lock()
    domains = append(domains, domain)
    // To avoid holding the mutex while writing to the
    // response body, make a local copy of the slice header.
    d := domains
    mu.Unlock()

    json.NewEncoder(w).Encode(d)
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/", CreateDomainEndpoint).Methods("POST")

    log.Fatal(http.ListenAndServe(":4000", handlers.CORS(handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}), handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"}), handlers.AllowedOrigins([]string{"*"}))(router)))
}
package main

import (
    "encoding/json"
    "log"
    "net/http"
    "sync"

    "github.com/gorilla/handlers"
    "github.com/gorilla/mux"
)

type Domain struct {
    Hostname string `json:"hostname,omitempty"`
}

var (
    domains []Domain
    mu      sync.Mutex
)

func CreateDomainEndpoint(w http.ResponseWriter, req *http.Request) {
    var domain Domain
    if err := json.NewDecoder(req.Body).Decode(&domain); err != nil {
        http.Error(w, "bad request", 400)
        return
    }
    mu.Lock()
    domains = append(domains, domain)
    // To avoid holding the mutex while writing to the
    // response body, make a local copy of the slice header.
    d := domains
    mu.Unlock()

    json.NewEncoder(w).Encode(d)
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/", CreateDomainEndpoint).Methods("POST")

    log.Fatal(http.ListenAndServe(":4000", handlers.CORS(handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}), handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"}), handlers.AllowedOrigins([]string{"*"}))(router)))
}