第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Content-Type multipart/form-data POST 數(shù)據(jù)

使用 Content-Type multipart/form-data POST 數(shù)據(jù)

Go
寶慕林4294392 2021-07-07 13:46:51
我正在嘗試使用 go 將圖像從我的計(jì)算機(jī)上傳到網(wǎng)站。通常,我使用 bash 腳本向服務(wù)器發(fā)送文件和密鑰:curl -F "image"=@"IMAGEFILE" -F "key"="KEY" URL它工作正常,但我正在嘗試將此請(qǐng)求轉(zhuǎn)換為我的 golang 程序。http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/我嘗試了這個(gè)鏈接和許多其他鏈接,但是,對(duì)于我嘗試的每個(gè)代碼,來(lái)自服務(wù)器的響應(yīng)是“沒(méi)有發(fā)送圖像”,我不知道為什么。如果有人知道上面的例子發(fā)生了什么。
查看完整描述

3 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是一些示例代碼。

簡(jiǎn)而言之,您需要使用該mime/multipart來(lái)構(gòu)建表單。

package main


import (

    "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 encode

    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

}


查看完整回答
反對(duì) 回復(fù) 2021-07-12
  • 3 回答
  • 0 關(guān)注
  • 432 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)