![a6dd6c3954cb69e88c682a7eabfeb201.png](https://img-blog.csdnimg.cn/img_convert/a6dd6c3954cb69e88c682a7eabfeb201.png)
通常我们与第三方交互使用的是 json,但偶尔也会遇到要求使用表单方式来提交数据,故今天我们就一起来学习下如何在 Go 中发送表单请求。
准备工作
首先我们有这样一段测试代码来接收 POST 请求,并返回其接收到的字段信息。
package mainimport ("fmt""log""net/http")func urlencodedHandler(w http.ResponseWriter, r *http.Request) {err := r.ParseForm()if err != nil {log.Printf("r.ParseForm(): %v", err)return}result := ""for k, v := range r.Form {result += fmt.Sprintf("%s:%v", k, v)}fmt.Fprintf(w, result)}func multipartHandler(w http.ResponseWriter, r *http.Request) {err := r.ParseMultipartForm(4 * 1024 * 1024)if err != nil {log.Printf("r.ParseForm(): %v", err)return}result := ""for k, v := range r.MultipartForm.Va