<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## Golang 發起 Get 請求 ### 基本的GET請求 ``` package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.Get("http://httpbin.org/get") if err != nil { fmt.Println(err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) fmt.Println(resp.StatusCode) if resp.StatusCode == 200 { fmt.Println("ok") } } ``` ### 帶參數的Get請求 ``` package main import ( "fmt" "io/ioutil" "net/http" ) func main(){ resp, err := http.Get("http://httpbin.org/get?name=zhaofan&age=23") if err != nil { fmt.Println(err) return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) } ``` 但是如果我們想要把一些參數做成變量而不是直接放到url中怎么操作,代碼例子如下: ``` package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main(){ params := url.Values{} Url, err := url.Parse("http://httpbin.org/get") if err != nil { return } params.Set("name","zhaofan") params.Set("age","23") //如果參數中有中文參數,這個方法會進行URLEncode Url.RawQuery = params.Encode() urlPath := Url.String() fmt.Println(urlPath) // https://httpbin.org/get?age=23&name=zhaofan resp,err := http.Get(urlPath) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) } ``` ### GET請求添加請求頭 ``` package main import ( "fmt" "io/ioutil" "net/http" ) func main() { client := &http.Client{} req,_ := http.NewRequest("GET","http://httpbin.org/get",nil) req.Header.Add("name","zhaofan") req.Header.Add("age","3") resp,_ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) fmt.Printf(string(body)) } ``` 從下面的結果可以看出我們設置的頭是成功了: ``` { "args": {}, "headers": { "Accept-Encoding": "gzip", "Age": "3", "Host": "httpbin.org", "Name": "zhaofan", "User-Agent": "Go-http-client/1.1" }, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/get" } ``` ## Golang發起 Post 請求 ### 基本的POST使用 ``` package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main() { urlValues := url.Values{} urlValues.Add("name","zhaofan") urlValues.Add("age","22") resp, _ := http.PostForm("http://httpbin.org/post",urlValues) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) } ``` 結果如下 ``` { "args": {}, "data": "", "files": {}, "form": { "age": "22", "name": "zhaofan" }, "headers": { "Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" } ``` 另外一種方式: ``` package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) func main() { urlValues := url.Values{ "name":{"zhaofan"}, "age":{"23"}, } reqBody:= urlValues.Encode() resp, _ := http.Post("http://httpbin.org/post", "text/html",strings.NewReader(reqBody)) body,_:= ioutil.ReadAll(resp.Body) fmt.Println(string(body)) } ``` 結果如下: ``` { "args": {}, "data": "age=23&name=zhaofan", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "text/html", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" } ``` ### 發送JSON數據的post請求 ``` package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "strings" ) func helloWorldJson1(b []byte) { fmt.Printf("request:%v\n", string(b)) resp, _ := http.Post("http://127.0.0.1:8080/helloworld", "text/thml", strings.NewReader(string(b))) body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("response:%v\n",string(body)) } func helloWorldJson2(b []byte) { fmt.Printf("request:%v\n", string(b)) client := &http.Client{} req, _ := http.NewRequest("POST", "http://127.0.0.1:8080/helloworld", bytes.NewReader(b)) resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("response:%v\n",string(body)) } func main() { data := make(map[string]interface{}) data["hello"] = "123123" data["world"] = "454544" bytesData, _ := json.Marshal(data) helloWorldJson1(bytesData) helloWorldJson2(bytesData) } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看