GO 发起HTTP请求调用接口

时间:2025-02-16 09:27:23

Go发起GET请求

基本的GET请求

//基本的GET请求
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    resp, err := ("http://")
    if err != nil {
        (err)
        return
    }
    defer ()
    body, err := ()
    (string(body))
    ()
    if  == 200 {
        ("ok")
    }
}

带参数的GET请求

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main(){
    resp, err := ("?name=Paul_Chan&age=26")
    if err != nil {
        (err)
        return
    }
    defer ()
    body, _ := ()
    (string(body))
}

如果我们想要把一些参数做成变量而不是直接放到url中怎么操作,代码例子如下:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main(){
    params := {}
    Url, err := ("")
    if err != nil {
        return
    }
    ("name","Paul_Chan")
    ("age","26")
    //如果参数中有中文参数,这个方法会进行URLEncode
     = ()
    urlPath := ()
    (urlPath) // ?age=26&name=Paul_chan
    resp,err := (urlPath)
    defer ()
    body, _ := ()
    (string(body))
}

解析JSON类型的返回结果

package main

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

type result struct {
    Args string `json:"args"`
    Headers map[string]string `json:"headers"`
    Origin string `json:"origin"`
    Url string `json:"url"`
}

func main() {
    resp, err := ("")
    if err != nil {
        return
    }
    defer ()
    body, _ := ()
    (string(body))
    var res result
    _ = (body,&res)
    ("%#v", res)
}

GET请求添加请求头

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client := &{}
    req,_ := ("GET","",nil)
    ("name","Paul_Chan")
    ("age","26")
    resp,_ := (req)
    body, _ := ()
    (string(body))
}

从上述的结果可以看出我们设置的头是成功了:

{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Age": "26", 
    "Host": "", 
    "Name": "Paul_Chan", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "origin": "211.138.20.170, 211.138.20.170", 
  "url": ""
}

golang 发起POST请求

基本的POST使用

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main() {
    urlValues := {}
    ("name","Paul_Chan")
    ("age","26")
    resp, _ := ("/post",urlValues)
    body, _ := ()
    (string(body))
}
//结果如下:
/******************
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "26", 
    "name": "Paul_Chan"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "19", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": null, 
  "origin": "211.138.20.170, 211.138.20.170", 
  "url": "/post"
}
******************/

另外一种方式

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
)

func main() {
    urlValues := {
        "name":{"Paul_Chan"},
        "age":{"26"},
    }
    reqBody:= ()
    resp, _ := ("/post", "text/html",(reqBody))
    body,_:= ()
    (string(body))
}
//结果如下:
/**************
{
  "args": {}, 
  "data": "age=26&name=Paul_Chan", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "19", 
    "Content-Type": "text/html", 
    "Host": "", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": null, 
  "origin": "211.138.20.170, 211.138.20.170", 
  "url": "/post"
}
***************/

发送JSON数据的post请求

package main

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

func main() {
    client := &{}
    data := make(map[string]interface{})
    data["name"] = "zhaofan"
    data["age"] = "23"
    bytesData, _ := (data)
    req, _ := ("POST","/post",(bytesData))
    resp, _ := (req)
    body, _ := ()
    (string(body))
}
//结果如下:
/*************
{
  "args": {}, 
  "data": "{\"age\":\"23\",\"name\":\"zhaofan\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "29", 
    "Host": "", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": {
    "age": "23", 
    "name": "zhaofan"
  }, 
  "origin": "211.138.20.170, 211.138.20.170", 
  "url": "/post"
}
*************/

不用client的post请求

package main

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

func main() {
    data := make(map[string]interface{})
    data["name"] = "zhaofan"
    data["age"] = "23"
    bytesData, _ := (data)
    resp, _ := ("/post","application/json", (bytesData))
    body, _ := ()
    (string(body))
}