解决golang post文件时Content-Type出现的问题
- 作者: 请叫我-宝哥哥
- 来源: 51数据库
- 2021-06-29
同事用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.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
