解決golang post文件時(shí)Content-Type出現(xiàn)的問(wèn)題
同事用php寫(xiě)了一個(gè)接口,要上傳文件,讓我做下測(cè)試,直接用curl命令調(diào)用成功,然后想用golang寫(xiě)個(gè)示例,
源碼如下:
package main import ( "bytes" "fmt" "io/ioutil" "mime/multipart" "net/http" ) func main() { uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商務(wù)提供 name := "xxxxxxxxxxxx" //用戶(hù)名 pass := "xxxxxxxxxxxx" //密碼 fn := "xxxxxxxxxxxx.txt" //文件路徑 //讀出文本文件數(shù)據(jù) file_data, _ := ioutil.ReadFile(fn) body := new(bytes.Buffer) w := multipart.NewWriter(body) //取出內(nèi)容類(lèi)型 content_type := w.FormDataContentType() //將文件數(shù)據(jù)寫(xiě)入 pa, _ := w.CreateFormFile("file", fn) pa.Write(file_data) //設(shè)置用戶(hù)名密碼 w.WriteField("name", name) w.WriteField("pass", pass) w.Close() //開(kāi)始提交 req, _ := http.NewRequest("POST", uri, body) req.Header.Set("Content-Type", content_type) resp, _ := http.DefaultClient.Do(req) data, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() fmt.Println(resp.StatusCode) fmt.Printf("%s", data) }
發(fā)現(xiàn)總是調(diào)用失敗,返回文件類(lèi)型不對(duì),詢(xún)問(wèn)后得知,同事做了判斷,文件只能為text/plain類(lèi)型,抓包發(fā)現(xiàn),我提交時(shí)的文件類(lèi)型為:application/octet-stream,仔細(xì)查看golang源碼:mime/multipart/write.go,CreateFormFile的源碼是這樣的:
func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error) { h := make(textproto.MIMEHeader) h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, escapeQuotes(fieldname), escapeQuotes(filename))) h.Set("Content-Type", "application/octet-stream") return w.CreatePart(h) }
可以得知Content-Type被固定為了application/octet-stream,知道原因了,問(wèn)題就好解決了。
第一種方法
就是直接修改CreateFormFile,或者加個(gè)CreateFormFile2命令,這種方法將來(lái)golang升級(jí)后可能會(huì)出問(wèn)題。
第二種方法
可以自己來(lái)CreatePart:
h := make(textproto.MIMEHeader) h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, escapeQuotes(fieldname), escapeQuotes(filename))) h.Set("Content-Type", "text/plain")
再用 w.CreatePart(h)得到io.Writer,問(wèn)題解決!這種方法不侵入golang源代碼,最終代碼如下:
package main import ( "bytes" "fmt" "io/ioutil" "mime/multipart" "net/http" "net/textproto" ) func main() { uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商務(wù)提供 name := "xxxxxxxxxx" //用戶(hù)名 pass := "xxxxxxx" //密碼 fn := "x:/xxx/xxx.txt" //文件路徑 //讀出文本文件數(shù)據(jù) file_data, _ := ioutil.ReadFile(fn) body := new(bytes.Buffer) w := multipart.NewWriter(body) //取出內(nèi)容類(lèi)型 content_type := w.FormDataContentType() //將文件數(shù)據(jù)寫(xiě)入 h := make(textproto.MIMEHeader) h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "file", //參數(shù)名為file fn)) h.Set("Content-Type", "text/plain") //設(shè)置文件格式 pa, _ := w.CreatePart(h) pa.Write(file_data) //設(shè)置用戶(hù)名密碼 w.WriteField("name", name) w.WriteField("pass", pass) w.Close() //開(kāi)始提交 req, _ := http.NewRequest("POST", uri, body) req.Header.Set("Content-Type", content_type) resp, _ := http.DefaultClient.Do(req) data, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() fmt.Println(resp.StatusCode) fmt.Printf("%s", data) }
補(bǔ)充:用go來(lái)玩最簡(jiǎn)單的web服務(wù)器------順便說(shuō)說(shuō)Content-Type字段
web服務(wù)端代碼s.go:
package main import ( "io" "log" "net/http" ) func handlerHello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello girls") } func main() { http.HandleFunc("/hello", handlerHello) // 注冊(cè) err := http.ListenAndServe("localhost:8080", nil) if err != nil { log.Println(err) } }
go run s.go一下,跑起來(lái), 然后在瀏覽器執(zhí)行http://127.0.0.1:8080/hello (或者在命令行用curl發(fā)http請(qǐng)求也可以), 瀏覽器上的結(jié)果為:
hello girls
好簡(jiǎn)單??梢栽诳蛻?hù)端或者服務(wù)端抓包看下, 很典型的http req和rsp.
我們?cè)賮?lái)看一個(gè)有趣的問(wèn)題, 修改s.go為:
package main import ( "io" "log" "net/http" ) func handlerHello(w http.ResponseWriter, r *http.Request) { str := ` table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table> ` io.WriteString(w, str) } func main() { http.HandleFunc("/hello", handlerHello) // 注冊(cè) err := http.ListenAndServe("localhost:8080", nil) if err != nil { log.Println(err) } }
再次重啟服務(wù)并發(fā)請(qǐng)求, 瀏覽器上顯示的內(nèi)容是:
table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table>
抓包看一下, 發(fā)現(xiàn)有:Content-Type: text/plain; charset=utf-8
因此, 瀏覽器需要根據(jù)純文本顯示。 注意到, 上述的table左邊少了一個(gè)"<". 我們加上后,
s.go的代碼如下:
package main import ( "io" "log" "net/http" ) func handlerHello(w http.ResponseWriter, r *http.Request) { str := ` <table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table> ` io.WriteString(w, str) } func main() { http.HandleFunc("/hello", handlerHello) // 注冊(cè) err := http.ListenAndServe("localhost:8080", nil) if err != nil { log.Println(err) } }
再次重啟服務(wù),發(fā)請(qǐng)求,瀏覽器端的顯示是:
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 | row 2, cell 2 |
抓包看, 有Content-Type: text/html; charset=utf-8
可見(jiàn), 服務(wù)端會(huì)判斷str的格式,來(lái)確定Content-Type的類(lèi)型, 從而決定了瀏覽器端的展示方式。服務(wù)端的自動(dòng)判斷行為, 有點(diǎn)意思。 在我看來(lái), 這樣不太好,應(yīng)該讓程序員來(lái)指定Content-Type.
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
詳解如何在Golang中監(jiān)聽(tīng)多個(gè)channel
這篇文章主要為大家詳細(xì)介紹了如何在Golang中實(shí)現(xiàn)監(jiān)聽(tīng)多個(gè)channel,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03Golang實(shí)現(xiàn)字符串倒序的幾種解決方案
給定一個(gè)字符串,按單詞將該字符串逆序是我們大家在開(kāi)發(fā)中可能會(huì)遇到的一個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于Golang如何實(shí)現(xiàn)字符串倒序的幾種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10Go Resiliency庫(kù)中timeout實(shí)現(xiàn)原理及源碼解析
Go-Resiliency庫(kù)中的timeout是一種基于協(xié)程的超時(shí)機(jī)制,通過(guò)創(chuàng)建協(xié)程來(lái)執(zhí)行任務(wù)并設(shè)置超時(shí)時(shí)間,若任務(wù)執(zhí)行時(shí)間超時(shí)則中止協(xié)程并返回錯(cuò)誤,需要詳細(xì)了解可以參考下文2023-05-05總結(jié)Go語(yǔ)言中defer的使用和注意要點(diǎn)
Go語(yǔ)言中的defer關(guān)鍵字實(shí)現(xiàn)比較特殊的功能,這篇文章給大家總結(jié)了關(guān)于Go語(yǔ)言中defer的使用和注意要點(diǎn),有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-09-09golang并發(fā)編程中Goroutine 協(xié)程的實(shí)現(xiàn)
Go語(yǔ)言中的協(xié)程是一種輕量級(jí)線(xiàn)程,通過(guò)在函數(shù)前加go關(guān)鍵字來(lái)并發(fā)執(zhí)行,具有動(dòng)態(tài)棧、快速啟動(dòng)和低內(nèi)存使用等特點(diǎn),本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下2024-10-10