Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

golang 有什么好办法解析 body 内容么?

这是代码,

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Println("header=", r.Header)

    s, \_ := ioutil.ReadAll(r.Body)
    fmt.Printf("body=%s\n",s)
}

这是返回值,

header= map[Accept-Encoding:[gzip] Connection:[close] Content-Length:[318512] Content-Type:[multipart/form-data; boundary=---------------------------8d7f730cd9470c6] Remote-Host:[183.250.162.102] User-Agent:[Go-http-client/1.1] X-Forwarded-For:[183.250.162.102, 120.76.211.89] X-Real-Ip:[183.250.162.102]]
body=-----------------------------8d7f730cd9470c6
Content-Disposition: form-data; name="bigFileMd5"

c73c0667b84b04bf3125643fac9331aa
-----------------------------8d7f730cd9470c6
Content-Disposition: form-data; name="cloudtoken"

267d1c6d911d57b64e6ea815818dbaad32cb4434e92234e42677caa5f344a332
-----------------------------8d7f730cd9470c6
Content-Disposition: form-data; name="deleteFile"

111101
-----------------------------8d7f730cd9470c6
Content-Disposition: form-data; name="fileCount"

11

网上的方案都是按照 json 来的,我这个明显不是。如果有现成的方法可以切割开最好,没有就只有手动写了,但我觉得还是不要造轮子比较好,我网上找了一下开源,也没有找到,初入 golang,还请指点一下,谢谢。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
fmt.Printf("PostFormValue(smallFileMd5)=%s
", r.PostFormValue("smallFileMd5"))
fmt.Printf("PostFormValue(deleteFile)=%s
", r.PostFormValue("deleteFile"))

file, _, err := r.FormFile("projectUploader")
if err != nil {
    fmt.Println(err)
}
defer file.Close()
s1, _ := ioutil.ReadAll(file)
fmt.Printf("filecontent=%s
",s1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...