2019-09-16 21:45
浏览 42


I'm not sure why the data being posted is not present when running the following curl request:

curl --request POST http://localhost:4000 --header "Content-Type: application/json" --data '{ "hostname": "bbc.co.uk" }'

against the code below. It's essentially just posting json with the variable hostname but for some reason it's not appearing in req.Body or appearing in the Domain structure array. Please note this is based on this tutorial

package main

import (


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

var domains []Domain

func CreateDomainEndpoint(w http.ResponseWriter, req *http.Request) {
    var domain Domain

    _ = json.NewDecoder(req.Body).Decode(&domain)
    domains = append(domains, domain)

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)))
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题