Go HTTP client, HTTP server

Source: Internet
Author: User

The HTTP client in the Go language, server is very simple. Specific as follows.

HTTP Server
package  mainimport (        "fmt"        "html"        "io/ioutil"        "log"        "net/http")func main() {        http.HandleFunc("/bar", func (w http.ResponseWriter, r *http.Request){                fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))                if r.Method == "POST" {                        b, err := ioutil.ReadAll(r.Body)                        if err != nil {                                log.Println("Read failed:", err)                        }                        defer r.Body.Close()                        log.Println("b:", string(b))                } else {                        log.Println("ONly support Post")                        w.Write([]byte("Only support post"))                }        })        log.Fatal(http.ListenAndServe(":8080", nil))}
HTTP Client
  1. Get method

      package Mainimport ("Io/ioutil" "Log" "Net/http") func main () {URL : = "Http://127.0.0.1:8080/bar" resp, err: = http. Get (URL) if err! = Nil {log. Println ("Get failed:", err) return} Defer resp. Body.close () If Resp. StatusCode! = http. Statusok {log. Println ("StatusCode:", resp. StatusCode)} content, err: = Ioutil. ReadAll (resp. Body) If err! = Nil {log. Println ("Read failed:", err)} log. Println ("Content:", string (content)}  
  2. Post mode

    package mainimport (        "bytes"        "io/ioutil"        "log"        "net/http")func main() {        url := "http://127.0.0.1:8080/bar"        contentType := "application/json;charset=utf-8"        b := []byte("Hello, Server")        body := bytes.NewBuffer(b)        resp, err := http.Post(url, contentType, body)        if err != nil {                log.Println("Post failed:", err)                return        }        defer resp.Body.Close()        content, err := ioutil.ReadAll(resp.Body)        if err != nil {                log.Println("Read failed:", err)                return        }        log.Println("content:", string(content))}

Attention:
Response the body after use remember close, namely:

Resp. Body.close ()

For information on how to pass JSON format data, you can refer to the blog Go http pass JSON data

Go HTTP client, HTTP server

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.