Golang Post传参调用示例

 Golang  Post传参

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

}


返回结果

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

猜你喜欢

转载自blog.51cto.com/dreamlinux/2405659