使用Content

使用Content,第1张

使用Content

这是一些示例代码。

简而言之,您将需要使用该

mime/multipart
软件包来构建表单

package mainimport (    "bytes"    "fmt"    "io"    "mime/multipart"    "net/http"    "net/http/httptest"    "net/http/httputil"    "os"    "strings")func main() {    var client *http.Client    var remoteURL string    {        //setup a mocked http client.        ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { b, err := httputil.DumpRequest(r, true) if err != nil {     panic(err) } fmt.Printf("%s", b)        }))        defer ts.Close()        client = ts.Client()        remoteURL = ts.URL    }    //prepare the reader instances to enpre    values := map[string]io.Reader{        "file":  mustOpen("main.go"), // lets assume its this file        "other": strings.NewReader("hello world!"),    }    err := Upload(client, remoteURL, values)    if err != nil {        panic(err)    }}func Upload(client *http.Client, url string, values map[string]io.Reader) (err error) {    // Prepare a form that you will submit to that URL.    var b bytes.Buffer    w := multipart.NewWriter(&b)    for key, r := range values {        var fw io.Writer        if x, ok := r.(io.Closer); ok { defer x.Close()        }        // Add an image file        if x, ok := r.(*os.File); ok { if fw, err = w.CreateFormFile(key, x.Name()); err != nil {     return }        } else { // Add other fields if fw, err = w.CreateFormField(key); err != nil {     return }        }        if _, err = io.Copy(fw, r); err != nil { return err        }    }    // Don't forget to close the multipart writer.    // If you don't close it, your request will be missing the terminating boundary.    w.Close()    // Now that you have a form, you can submit it to your handler.    req, err := http.NewRequest("POST", url, &b)    if err != nil {        return    }    // Don't forget to set the content type, this will contain the boundary.    req.Header.Set("Content-Type", w.FormDataContentType())    // Submit the request    res, err := client.Do(req)    if err != nil {        return    }    // Check the response    if res.StatusCode != http.StatusOK {        err = fmt.Errorf("bad status: %s", res.Status)    }    return}func mustOpen(f string) *os.File {    r, err := os.Open(f)    if err != nil {        panic(err)    }    return r}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4899006.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存