Golang send get and post examples

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

GETRequest

GET requests can be used directly with http.Get methods

Simple

func main(){resp, err := http.Get("https://baidu.com")    if err != nil {        panic(err)        }    defer resp.Body.Close()    s,err:=ioutil.ReadAll(resp.Body)    fmt.Printf(string(s))}

Complex

func main() {    params := url.Values{}    Url, err := url.Parse("http://baidu.com?fd=fdsf")    if err != nil {        panic(err.Error())    }    params.Set("a", "fdfds")    params.Set("id", string("1"))    //如果参数中有中文参数,这个方法会进行URLEncode    Url.RawQuery = params.Encode()    urlPath := Url.String()    resp, err := http.Get(urlPath)    defer resp.Body.Close()    s, err := ioutil.ReadAll(resp.Body)    fmt.Println(string(s))}

This is params.set not feeling with PHP http_build_query , and I feel ha

POSTRequest

Use http.post
type Server struct {    ServerName string    ServerIp   string}type ServerSlice struct {    Server    []Server    ServersID string}func main() {    //post 第三个参数是io.reader interface    //strings.NewReader  byte.NewReader bytes.NewBuffer  实现了read 方法    s := ServerSlice{ServersID: "tearm", Server: []Server{{"beijing", "127.0.0.1"}, {"shanghai", "127.0.0.1"}}}    b, _ := json.Marshal(s)         fmt.Println(string(b))    resp, _ := http.Post("http://baidu.com", "application/x-www-form-urlencoded", strings.NewReader("heel="+string(b)))    //    defer resp.Body.Close()    //io.Reader    body, _ := ioutil.ReadAll(resp.Body)    fmt.Println(string(body))
Use http.PostForm
func httpPostForm() {// params:=url.Values{}// params.Set("hello","fdsfs")  //这两种都可以   params= url.Values{"key": {"Value"}, "id": {"123"}}     resp, _:= http.PostForm("http://baidu.com",       body)     defer resp.Body.Close()    body, _:= ioutil.ReadAll(resp.Body)        fmt.Println(string(body)) }

If you need to set the header parameters, cookies and other data, you can use thehttp.Do

func httpDo() {    client := &http.Client{}        req, err := http.NewRequest("POST", "baidu.com", strings.NewReader("name=cjb"))    if err != nil {        // handle error    }     req.Header.Set("Content-Type", "application/x-www-form-urlencoded")    req.Header.Set("Cookie", "name=anny")     resp, err := client.Do(req)     defer resp.Body.Close()     body, err := ioutil.ReadAll(resp.Body)    if err != nil {        // handle error    }     fmt.Println(string(body))}

The same http.NewRequest third parameter only needs to implement the io.reader interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.