1. multipart 相关
multipart/form-data的基础方法是post,其请求头必须包含一个特殊的头信息:Content-Type,值为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容。举例为:content-type :multipart/form-data; boundary=b626cfbb33bfba2d6b11642964898304973bd84207dd66f2231fb97d26eb
请求体样式
--{bound}--
Content-Disposition: form-data; name="file1"; filename="file1“
Content-Type: application/octet-stream
file content
--{bound}--
--{bound}
Content-Disposition: form-data; name="XXXX"
Submit Query
--{bound}--
如果为文件,不要忘记加上Content-Type: application/octet-stream
2. 例子代码
//参数为本地文件地址
func NewUploadRequest1(url string, params map[string]string, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// 实例化multipart
body