服务端
package main import ( "encoding/json" "net/http" "zdpgo_chi" "zdpgo_chi/middleware" ) func main() { r := zdpgo_chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.RealIP) r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Get("/", func(w http.ResponseWriter, r *http.Request) { // 获取查询参数 name := r.URL.Query().Get("name") age := r.URL.Query().Get("age") data := map[string]string{ "name": name, "age": age, } // 获取请求头参数 data["accept"] = r.Header.Get("Accept") data["authorization"] = r.Header.Get("Authorization") // 返回JSON数据 jsonData, err := json.Marshal(data) if err != nil { w.Write([]byte(err.Error())) return } w.Write(jsonData) }) http.ListenAndServe(":3333", r) }
客户端
package main import ( "fmt" "zdpgo_resty" ) func main() { // 創建客戶端 client := zdpgo_resty.New() // 发送请求,获取响应 resp, err := client.R(). SetQueryParams(map[string]string{ // 设置请求参数 "name": "张大鹏", "age": "22", }). SetHeader("Accept", "application/json"). // 设置请求头 SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F"). // 设置请求Token Get("http://127.0.0.1:3333/") // 查看响应结果 if err != nil { fmt.Println(err) return } fmt.Println(resp.String()) }
还没有评论,来说两句吧...