Go language Web programming Get and Post request to send and parse method details

  • 2020-06-03 06:56:04
  • OfStack

The example of this paper describes the Go language Web programming Get and Post request sending and parsing methods. To share for your reference, specific as follows:

This is a primer on the main techniques used in Golang's Web programming with a simple example.

The structure of the paper includes:

1. Client - Get request
2. Client - Post request
3. Server handles Get and Post data

In data encapsulation, we use json in part, so this paper also involves the encoding and decoding of json in Golang.

1. Client-Get

package main
import (
        "fmt"
        "net/url"
        "net/http"
        "io/ioutil"
        "log"
)
func main() {
        u, _ := url.Parse("http://localhost:9001/xiaoyue")
        q := u.Query()
        q.Set("username", "user")
        q.Set("password", "passwd")
        u.RawQuery = q.Encode()
        res, err := http.Get(u.String());
        if err != nil {
              log.Fatal(err) return
        }
        result, err := ioutil.ReadAll(res.Body)
        res.Body.Close()
        if err != nil {
              log.Fatal(err) return
        }
        fmt.Printf("%s", result)
}

2. Client-Post

package main
import (
        "fmt"
        "net/url"
        "net/http"
        "io/ioutil"
        "log"
        "bytes"
        "encoding/json"
)
type Server struct {
        ServerName string
        ServerIP   string
}
type Serverslice struct {
        Servers []Server
        ServersID  string
}
func main() {
        var s Serverslice
        var newServer Server;
        newServer.ServerName = "Guangzhou_VPN";
        newServer.ServerIP = "127.0.0.1"
        s.Servers = append(s.Servers, newServer)
        s.Servers = append(s.Servers, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.2"})
        s.Servers = append(s.Servers, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.3"})
        s.ServersID = "team1"
        b, err := json.Marshal(s)
        if err != nil {
                fmt.Println("json err:", err)
        }
        body := bytes.NewBuffer([]byte(b))
        res,err := http.Post("http://localhost:9001/xiaoyue", "application/json;charset=utf-8", body)
        if err != nil {
                log.Fatal(err)
                return
        }
        result, err := ioutil.ReadAll(res.Body)
        res.Body.Close()
        if err != nil {
                log.Fatal(err)
                return
        }
        fmt.Printf("%s", result)
}

3. Server

package main
import (
        "fmt"
        "net/http"
        "strings"
        "html"
        "io/ioutil"
        "encoding/json"
)
type Server struct {
        ServerName string
        ServerIP   string
}
type Serverslice struct {
        Servers []Server
        ServersID  string
}
func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":9001", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
        r.ParseForm() // Parse parameter, default will not parse
        fmt.Fprintf(w, "Hi, I love you %s", html.EscapeString(r.URL.Path[1:]))
        if r.Method == "GET" {
                fmt.Println("method:", r.Method) // Get the method of the request
                fmt.Println("username", r.Form["username"])
                fmt.Println("password", r.Form["password"])
                for k, v := range r.Form {
                        fmt.Print("key:", k, "; ")
                        fmt.Println("val:", strings.Join(v, ""))
                }
        } else if r.Method == "POST" {
                result, _:= ioutil.ReadAll(r.Body)
                r.Body.Close()
                fmt.Printf("%s\n", result)
                // Recommended handling for unknown types
                var f interface{}
                json.Unmarshal(result, &f)
                m := f.(map[string]interface{})
                for k, v := range m {
                        switch vv := v.(type) {
                                case string:
                                        fmt.Println(k, "is string", vv)
                                case int:
                                        fmt.Println(k, "is int", vv)
                                case float64:
                                        fmt.Println(k,"is float64",vv)
                                case []interface{}:
                                        fmt.Println(k, "is an array:")
                                        for i, u := range vv {
                                                fmt.Println(i, u)
                                        }
                                default:
                                        fmt.Println(k, "is of a type I don't know how to handle")
                         }
                  }
                 // We know the structure, and we resolve it to the structure
                 var s Serverslice;
                 json.Unmarshal([]byte(result), &s)
                 fmt.Println(s.ServersID);
                 for i:=0; i<len(s.Servers); i++ {
                         fmt.Println(s.Servers[i].ServerName)
                         fmt.Println(s.Servers[i].ServerIP)
                 }
        }
}

I hope this article has been helpful in Go programming.


Related articles: