核心代码
这里通过字典传递了一个简单的表单数据。
发送的是POST请求。
resp, err := client.R().SetFormData(map[string]string{"username": "jeeva","password": "mypass",}).Post("http://127.0.0.1:3333/login")fmt.Println(resp, err)
服务端
package mainimport ("encoding/json""fmt""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.Post("/login", func(w http.ResponseWriter, r *http.Request) {r.ParseForm()username := r.Form.Get("username")password := r.Form.Get("password")search := r.Form.Get("search")fmt.Println(username, password, search)data := map[string]interface{}{"username": username,"password": password,"search": search,}jsonData, err := json.Marshal(data)if err != nil {w.Write([]byte(err.Error()))return}w.Write(jsonData)})http.ListenAndServe(":3333", r)
}
客户端
package mainimport ("fmt""net/url""zdpgo_resty"
)func main() {client := zdpgo_resty.New()// 使用表单登录resp, err := client.R().SetFormData(map[string]string{"username": "jeeva","password": "mypass",}).Post("http://127.0.0.1:3333/login")fmt.Println(resp, err)// 传递数组criteria := url.Values{"search": []string{"book", "glass", "pencil"},}resp, err = client.R().SetFormData(map[string]string{"username": "jeeva","password": "mypass",}).SetFormDataFromValues(criteria).Post("http://127.0.0.1:3333/login")fmt.Println(resp, err)
}