2017-09-01 4 views
0

URLからいくつかのcsvデータをダウンロードしようとしています。生の応答は今、私はこれは本当にマルチパート応答ではありません実現が、それはそれは、ブラウザによってダウンロードとして扱われる必要があると言うContent-Disposition: attachment; filename="GooglePLAv1US.txt"ヘッダを、持っているこのHTTP応答から添付ファイルを読み取る方法

HTTP/1.1 200 OK 
Server: Europa-4 
X-Varnish: 33948791 
Vary: Accept-Encoding, X-UA-Device 
X-Cache: MISS 
Cache-Control: no-cache, no-cache, no-store, proxy-revalidate, must-revalidate, max-age=0 
Content-Type: application/octet-stream 
P3p: CP="CAO PSA OUR" 
Date: Fri, 01 Sep 2017 19:53:27 GMT 
X-Server: web03 
Expires: Fri, 01 Sep 2017 19:53:26 GMT 
X-XSS-Protection: 1; mode=block 
Transfer-Encoding: chunked 
Accept-Ranges: bytes 
X-Content-Type-Options: nosniff 
Content-Disposition: attachment; filename="GooglePLAv1US.txt" 
Via: 1.1 varnish-v4 
Connection: keep-alive 
Last-Modified: Fri, 01 Sep 2017 19:53:27 +0000 
X-Frame-Options: sameorigin 
X-UA-Device: desktop 
Age: 0 
X-Modified-Url: /amfeed/main/get/file/GooglePLAv1US/?___store=ca_en 

id  title description  google_product_category ..... 
20649074  ...... 
20652632  ...... 
. 
. 
. 

ようになります。

応答の本文を読み込もうとすると、エラーunexpected EOFがスローされます。このデータをどのように読み取るのですか。それは実際にはどのようなセクションでもありません。

コード

http.DefaultClient.Timeout = time.Second * 30 
resp, err := http.Get(ht.Creds.AccessData.URL) 
if err != nil { 
    return nil, err 
} 
defer resp.Body.Close() 
d, err := ioutil.ReadAll(resp.Body) 
if err != nil { 
    return nil, errors.Wrap(err, "Error reading HTTP response body") 
} 

は、これはここで、

Error reading HTTP response body: unexpected EOF 
+0

Go(質問タグに基づいて)を使用していると仮定しても、コードは表示されていないので、誰でも提供できるヘルプはほとんどありません。 – Adrian

+0

@Adrianが私が使用しているコードを提出しました、ありがとう! – Danish

答えて

0

net/textprotoに試してみて、エラーが発生し、小さな例です。

package main 

import (
    "bufio" 
    "fmt" 
    "io" 
    "net/http" 
    "net/http/httptest" 
    "net/textproto" 
    "os" 
) 

func main() { 
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
     fmt.Fprintf(w, "%s\n%s\n%s\n%s\n", 
      "col1,col2,col3", 
      "1,2,3", 
      "a,b,c", 
      "x,y,x", 
     ) 
    })) 
    defer ts.Close() 

    client := &http.Client{} 
    res, err := client.Get(ts.URL) 
    if err != nil { 
     fmt.Fprintln(os.Stderr, err) 
     return 
    } 
    defer res.Body.Close() 
    reader := bufio.NewReader(res.Body) 
    tp := textproto.NewReader(reader) 
    for { 
     if line, err := tp.ReadLine(); err != nil { 
      if err == io.EOF { 
       // if file is emtpy 
       return 
      } 
      return 
     } else { 
      fmt.Printf("%s\n\n", line) 
     } 
    } 
} 

https://play.golang.org/p/fee4B5mh35

ここ

は、「CSV」に関するあなたの元の質問に基づいて別の例です。この場合は

package main 

import (
    "bufio" 
    "fmt" 
    "net/http" 
    "os" 
) 

func main() { 
    client := &http.Client{} 
    res, err := client.Get("http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv") 
    if err != nil { 
     fmt.Fprintln(os.Stderr, err) 
     return 
    } 
    defer res.Body.Close() 
    scanner := bufio.NewScanner(res.Body) 
    scanner.Split(bufio.ScanBytes) 
    for scanner.Scan() { 
     c := scanner.Text() 
     switch c { 
     case "\r": 
      fmt.Println() 
     default: 
      fmt.Printf("%s", c) 
     } 
    } 
} 

scanner.Split(bufio.ScanBytes)に気づく、これはあなたがより多くのアイデアを与えることができることを望みます。

+0

興味深いことに、私の元のコードでも、もし私が 'err == io 'をチェックすれば。 ErrUnexpectedEOF'をダウンロードして無視してください。ダウンロードしたデータは、ブラウザがダウンロードしたものと全く同じです。 – Danish

+0

https://golang.org/pkg/mime/multipart/を試しましたか? – nbari

関連する問題