美文网首页
Go中构建Http请求参数

Go中构建Http请求参数

作者: 四维构思 | 来源:发表于2020-12-04 10:17 被阅读0次
    func createMultipartRequest() *http.Request {
        boundary := "--testboundary"
        body := new(bytes.Buffer)
        mw := multipart.NewWriter(body)
        defer mw.Close()
    
        must(mw.SetBoundary(boundary))
        must(mw.WriteField("foo", "bar"))
        must(mw.WriteField("bar", "10"))
        must(mw.WriteField("bar", "foo2"))
        must(mw.WriteField("array", "first"))
        must(mw.WriteField("array", "second"))
        must(mw.WriteField("id", ""))
        must(mw.WriteField("time_local", "31/12/2016 14:55"))
        must(mw.WriteField("time_utc", "31/12/2016 14:55"))
        must(mw.WriteField("time_location", "31/12/2016 14:55"))
        must(mw.WriteField("names[a]", "thinkerou"))
        must(mw.WriteField("names[b]", "tianou"))
        req, err := http.NewRequest("POST", "/", body)
        must(err)
        req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
        return req
    }
    

    相关文章

      网友评论

          本文标题:Go中构建Http请求参数

          本文链接:https://www.haomeiwen.com/subject/jjpbwktx.html