go实现http的Get和Post请求

设置header和body的post请求

type Tokenstruct struct {
    Access_token string
    Token_type string
}

func (ts *Tokenstruct) DnsGettoken() (string){
    tokenurl := "url地址"

    payload := strings.NewReader("body信息")
    client := &http.Client{}
    req,err := http.NewRequest(http.MethodPost,tokenurl,payload)
    if err != nil{
        fmt.Println(err)
        return ""
    }
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    res, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return ""
    }
    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println(err)
        return ""
    }

   //json数据解析成结构体
    json.Unmarshal(body,ts)
    return ts.Access_token
}

设置请求头和不设置请求头请求body的get请求

type Detailstruct struct {
    Count int
    Items []struct{
        Id int
        Name string
        Tags string
        Product_id int
        Product_name string
        Zone_id int
        Zone_name string
        Record_count int
        Service_status int
        Manager string
        Second_contact string
        Principal string
        Locked bool
        Is_important bool
    }
}
type DomainIdstuct struct {
    Code int
    Msg string
    Details Detailstruct
}


func (dallinfo *Domainipinfostuct) Printdomaininfo(inputdomainname string,domainid int,dnstoken string) (map[int]map[string]int){
    var domain_alllinedict map[int]map[string]int = make(map[int]map[string]int,0)

    url := fmt.Sprintf("请求url")
    client := &http.Client{Timeout:5*time.Second}
    payload := strings.NewReader(``)
    req,err := http.NewRequest(http.MethodGet,url,payload)

    if err != nil{
        fmt.Println(err)
    }
    req.Header.Add("Authorization", fmt.Sprintf("Bearer %s",dnstoken))
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    res, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
    }
    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println(err)
    }

    json.Unmarshal(body,&dallinfo)
}

即设置header又设置body并解析

type Allstruct struct {
    Code int
    Msg string
    Details struct{
        Operation_id int
    }
}

type AllOtherstruct struct{
    Code int
    Msg string
    Details struct{
        Code int
        Msg string
        Details struct{
            Operation_id int
        }
    }
}

func PostallJson(alljson map[string][]interface{},dnstoken string){
    allurl := "请求url"

    allstr,_ := json.Marshal(alljson)    //map结构转换为字符切片
    payload := strings.NewReader(string(allstr))
    client := &http.Client{}
    req,err := http.NewRequest(http.MethodPost,allurl,payload)
    if err != nil{
        fmt.Println("1",err)
    }
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Add("Authorization", fmt.Sprintf("Bearer %s",dnstoken))
    res, err := client.Do(req)
    if err != nil {
        fmt.Println("2",err)
    }
    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("3",err)
    }

    //fmt.Println(string(body))

    defer func() {
        if r := recover(); r != nil {
            var allotherinfo *AllOtherstruct
            allotherinfo = new(AllOtherstruct)
            json.Unmarshal(body,allotherinfo)
            fmt.Println(*allotherinfo)
        }
    }()

    var allinfo *Allstruct
    allinfo = new(Allstruct)
    json.Unmarshal(body,allinfo)
    fmt.Println(*allinfo)
}

你可能感兴趣的:(go实现http的Get和Post请求)