you have to consume the body of the request.
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)
if you have to deal with a multipart form data https://golang.org/pkg/net/http/#Request.FormFile
Given following comment,
i see now after printing resp that there is a csv text but type is
http.Response i have to deal with golang.org/pkg/encoding/csv/#Reader
how to turn resp to string in order to be able reader to read it, or i
miss something else ?
OP has to understand that an http response Body implements the io.Reader
interface.
When the http response come back from the server, the body is not read directly into memory as slice of bytes []byte
.
OP should note also that a csv.Reader
is an implementation to decode a CSV encoded content that consumes an io.Reader
. Under the hood it does not hold the entire content of the file in memory, it reads whats needed to decode one line and proceed further.
As a consequence to those two important properties of golang implementation, is that it is easy and natural to connect the response body reader to the csv reader.
To the question, what is an io.Reader
, OP has to figure out it is anything capable of reading a byte stream by chunks of max len p. This is illustrated by the signature of the unique method of this interface Read([]byte) (int, error)
This interface is designed in such way that it minimizes consumed memory and allocations.
ref links
All that being said, the final code is trivially written,
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
)
func main() {
baseUrl := "https://geolite.maxmind.com/download/geoip/misc/region_codes.csv"
client := http.Client{}
resp, err := client.Get(baseUrl)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
fmt.Println(resp)
r := csv.NewReader(resp.Body)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(record)
}
}