Golang Post傳參調用示例

 Golang  Post傳參css

package main

import (
   "fmt"
   "net/http"
   "io/ioutil"
   "strings"
   "encoding/json"
   "log"
)

type  TeamworkInfo struct {
   ProductId int64 `json:"productId"`     //產品ID
   Topic string `json:"topic"`    //
   Environment  int64 `json:"environment"`    //調用環境生產爲1 沙盒2
   DiscoverType  int64 `json:"discoverType"`  //缺陷發現方式 告警 4
   Manager string   `json:"manager"`         //負責人
   Creator string  `json:"creator"`    //建立者
   Priority int64 `json:"priority"`   //優先級
   Description string  `json:"description"`   //故障描述詳情
   Members    []string `json:"members"`   //成員
   Tags   []string  `json:"tags"`     //標籤
   Reason string  `json:"reason"`     //故障緣由
}

func main() {

   url := "http://10.57.127.111:8088/api/rest/defect/create"
   teamworkinfo :=TeamworkInfo{
      Topic:"[網絡質量監控平臺異常]",
      ProductId:211,
      Creator:"abc",
      Description:"網絡質量監控 北京機房網絡丟包",
      DiscoverType:4,
      Environment:1,
      Manager:"anxiang.peng",
      Priority:1,
      Members:[]string{"abc","bcd","oncall1"},
      Tags:[]string{"基建數據異常","第三方數據異常"},
      Reason:"網絡抖動",

   }
   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))

}


返回結果json

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

相關文章
相關標籤/搜索