如何从http.Request的响应中读取文件内容

问题描述 投票:-2回答:1

我使用下面的代码将请求发送到http服务器。

服务器发送包含那些http标头的响应

Content-Disposition:[attachment;filename=somefilename.csv] 
Content-Type:[text/csv; charset=UTF-8]

我如何继续检索响应所附文件的内容?

baseUrl := "Some url that i call to fetch csv file"

client := http.Client{}

resp, _ := client.Get(baseUrl)
defer resp.Body.Close()

fmt.Println(resp)

// &{200 OK 200 HTTP/2.0 2 0 map[Content-Disposition:[attachment;filename=somefilename.csv] Content-Type:[text/csv; charset=UTF-8] Date:[Mon, 30 Sep 2019 09:54:08 GMT] Server:[Jetty(9.2.z-SNAPSHOT)] Vary:[Accept]] {0xc000530280} -1 [] false false map[] 0xc000156200 0xc0000c26e0}
go multipart content-disposition
1个回答
0
投票

您必须消耗请求的主体。

baseUrl := "Some url that i call to fetch csv file"

client := http.Client{}

resp, _ := client.Get(baseUrl)
defer resp.Body.Close()
io.Copy(os.Stdout, resp.Body) // this line.

fmt.Println(resp)

如果必须处理多部分表格数据https://golang.org/pkg/net/http/#Request.FormFile

© www.soinside.com 2019 - 2024. All rights reserved.