Golang Post calls pass parameters example

 Golang Post parameter passing

main Package Penalty for 

Import ( 
   "fmt" 
   "NET / HTTP" 
   "IO / ioutil" 
   "strings" 
   "encoding / json" 
   "log" 
) 

of the type struct {TeamworkInfo 
   ProductId int64 `json:" productId "` // Product ID 
   Topic String `json : "Topic" `// 
   environment int64` json: "environment" `// call the production environment sandbox 1 2 
   DiscoverType int64` json: "discoverType" `// defects found ways alarm 4 
   Manager String` json: "Manager" `// Leader 
   Creator string` json: "creator" `// creator 
   priority int64` json: "priority" `// priority 
   Description string` json: "description "` // fault Description Details 
   Members [] string `json:" members "` // members
   Tags [] string `json:" tags "` // tag 
   Reason string `json:" reason " ` // Faulty 
} 

FUNC main () { 

   URL: = "http://10.57.127.111:8088/api/rest / Defect / Create " 
   teamworkinfo: = {TeamworkInfo 
      Topic:" [internet network quality monitoring abnormal] ", 
      the ProductId: 211, 
      Creator:" ABC ", 
      the Description:" monitoring network quality room Beijing network packet loss ", 
      DiscoverType:. 4, 
      Environment :. 1, 
      Manager: "anxiang.peng", 
      the Priority:. 1, 
      Members: [] {String "ABC", "BCD", "oncall1"}, 
      Tags: [] {String "infrastructure data is abnormal", "third-party data abnormal "}, 
      Reason:" network jitter "

   }
   jsons,_:=json.Marshal(teamworkinfo)
   result :=string(jsons) 
   jsoninfo:=strings.NewReader(result)

   req, _ := http.NewRequest("POST", url,jsoninfo)
   req.Header.Add("appCode", "winner")
   req.Header.Add("token", "466221e4-593d-4bb8-b41b-hcfedhf")//应用对接的token
   req.Header.Add("Content-Type", "application/json")
   res, err := http.DefaultClient.Do(req)
   if  err !=nil{
      log.Printf("调用接口异常%v",err.Error())
   }
   defer res.Body.Close()
   body, err := ioutil.ReadAll(res.Body)
   if  err !=nil{
      log.Printf("调用接口异常%v",err.Error())
   }

   //fmt.Println(res)
   fmt.Println(string(body))

}


Back to Results

{"code":10100,"data":981,"msg":"成功","success":true,"meta":null}

Guess you like

Origin blog.51cto.com/dreamlinux/2405659