当前位置 博文首页 > 解决golang post文件时Content-Type出现的问题

    解决golang post文件时Content-Type出现的问题

    作者:九江Mgx 时间:2021-05-27 17:46

    同事用php写了一个接口,要上传文件,让我做下测试,直接用curl命令调用成功,然后想用golang写个示例,

    源码如下:

    package main 
    import (
        "bytes" 
        "fmt" 
        "io/ioutil" 
        "mime/multipart" 
        "net/http" 
    )
     
    func main() { 
        uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商务提供 
        name := "xxxxxxxxxxxx" //用户名 
        pass := "xxxxxxxxxxxx" //密码 
        fn := "xxxxxxxxxxxx.txt" //文件路径
     
        //读出文本文件数据 
        file_data, _ := ioutil.ReadFile(fn) 
        body := new(bytes.Buffer) 
        w := multipart.NewWriter(body)
     
        //取出内容类型 
        content_type := w.FormDataContentType() 
        //将文件数据写入 
        pa, _ := w.CreateFormFile("file", fn) 
        pa.Write(file_data) 
        //设置用户名密码 
        w.WriteField("name", name) 
        w.WriteField("pass", pass) 
        w.Close() 
        //开始提交
     
        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) 
    }
    

    发现总是调用失败,返回文件类型不对,询问后得知,同事做了判断,文件只能为text/plain类型,抓包发现,我提交时的文件类型为:application/octet-stream,仔细查看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,知道原因了,问题就好解决了。

    第一种方法

    就是直接修改CreateFormFile,或者加个CreateFormFile2命令,这种方法将来golang升级后可能会出问题。

    第二种方法

    可以自己来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,问题解决!这种方法不侵入golang源代码,最终代码如下:

    package main 
    import (
        "bytes"
        "fmt"
        "io/ioutil"
        "mime/multipart"
        "net/http"
        "net/textproto"
    )
     
    func main() {
        uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商务提供
        name := "xxxxxxxxxx"                      //用户名
        pass := "xxxxxxx"                         //密码
        fn := "x:/xxx/xxx.txt"                    //文件路径
     
        //读出文本文件数据
        file_data, _ := ioutil.ReadFile(fn)
     
        body := new(bytes.Buffer)
        w := multipart.NewWriter(body)
     
        //取出内容类型
        content_type := w.FormDataContentType()
     
        //将文件数据写入
        h := make(textproto.MIMEHeader)
        h.Set("Content-Disposition",
            fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
                "file", //参数名为file
                fn))
        h.Set("Content-Type", "text/plain") //设置文件格式
        pa, _ := w.CreatePart(h)
        pa.Write(file_data)
     
        //设置用户名密码
        w.WriteField("name", name)
        w.WriteField("pass", pass)
     
        w.Close() 
        //开始提交
        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)
    }

    补充:用go来玩最简单的web服务器------顺便说说Content-Type字段

    web服务端代码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)     // 注册   
        err := http.ListenAndServe("localhost:8080", nil)
        if err != nil {
            log.Println(err)
        }
    }

    go run s.go一下,跑起来, 然后在浏览器执行http://127.0.0.1:8080/hello (或者在命令行用curl发http请求也可以), 浏览器上的结果为:

    hello girls

    好简单。可以在客户端或者服务端抓包看下, 很典型的http req和rsp.

    我们再来看一个有趣的问题, 修改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)     // 注册   
        err := http.ListenAndServe("localhost:8080", nil)
        if err != nil {
            log.Println(err)
        }
    }

    再次重启服务并发请求, 浏览器上显示的内容是:

    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>

    抓包看一下, 发现有:Content-Type: text/plain; charset=utf-8

    因此, 浏览器需要根据纯文本显示。 注意到, 上述的table左边少了一个"<". 我们加上后,

    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)     // 注册   
        err := http.ListenAndServe("localhost:8080", nil)
        if err != nil {
            log.Println(err)
        }
    }

    再次重启服务,发请求,浏览器端的显示是:

    row 1, cell 1 row 1, cell 2
    row 2, cell 1 row 2, cell 2

    抓包看, 有Content-Type: text/html; charset=utf-8

    可见, 服务端会判断str的格式,来确定Content-Type的类型, 从而决定了浏览器端的展示方式。服务端的自动判断行为, 有点意思。 在我看来, 这样不太好,应该让程序员来指定Content-Type.

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持站长博客。如有错误或未考虑完全的地方,望不吝赐教。

    js
    下一篇:没有了