2017-05-19 68 views
1

我使用net/http庫在「開始」作出HTTP GET請求。在迴應中,我得到了12個標題。但是,當我通過郵遞員運行完全相同的查詢時,我得到了16個標題。其中一個遺漏的是「內容編碼」。我明白這一定是CORS問題。轉到:檢測gzip編碼手動解壓縮響應,但「內容編碼」標頭丟失

但是因爲我沒有在我的請求中設置標頭Accept-Encoding: gzip,而且我仍然得到gzip編碼的響應,所以Go運輸不是automatically decompressing the response for me。所以,我需要能夠手動檢測編碼,然後解壓縮它。但是,我無法檢測到響應中是否缺少'Content-Encoding'標頭。

這裏是我的代碼,我嘗試這樣做:

func calcDistanceAndDurationWithUberApi(originLat float64, originLon float64, destinationLat float64, destinationLon float64) (float64, float64, error) { 

    endpoint := "https://api.uber.com/v1.2/estimates/price" 
    parameters := fmt.Sprintf("?start_latitude=%v&start_longitude=%v&end_latitude=%v&end_longitude=%v", originLat, originLon, destinationLat, destinationLon) 

    req, err := http.NewRequest("GET", endpoint + parameters, nil) 
    if err != nil { 
     return 0, 0, err 
    } 

    req.Header.Add("Authorization", "Token " + getUberApiKey()) 
    req.Header.Add("Accept-Language", "en_US") 
    req.Header.Add("Content-Type", "application/json") 

    httpClient := &http.Client{} 
    resp, err := httpClient.Do(req) 
    if err != nil { 
     return 0, 0, err 
    } 
    if resp.StatusCode != 200 { 
     return 0, 0, errors.NotFound("Response: %v", resp.StatusCode) 
    } 
    defer resp.Body.Close() 

    pretty.Println("- REQUEST: ") 
    pretty.Println(req) 

    // Check if server sent gzipped response. Decompress if yes. 
    var respReader io.ReadCloser 
    switch resp.Header.Get("Content-Encoding") { 
    case "gzip": 
     fmt.Println("Content-Encoding is gzip") 
     respReader, err = gzip.NewReader(resp.Body) 
     defer respReader.Close() 
    default: 
     fmt.Println("Content-Encoding is Not gzip") 
     respReader = resp.Body 
    } 

    pretty.Println("- RESPONSE HEADER: ") 
    pretty.Println(resp.Header) 

    pretty.Println("- RESPONSE BODY: ") 
    pretty.Println(respReader) 

    return 0, 0, nil 
} 

響應狀態是 '200 OK'。這裏是輸出(響應):我給到超級API的頑固和加入另一個請求頭,req.Header.Add("Accept-Encoding", "gzip")

- RESPONSE HEADER: 
http.Header{ 
    "Content-Language":   {"en"}, 
    "Cache-Control":    {"max-age=0"}, 
    "X-Uber-App":    {"uberex-nonsandbox", "optimus"}, 
    "Strict-Transport-Security": {"max-age=604800", "max-age=2592000"}, 
    "X-Content-Type-Options": {"nosniff"}, 
    "Date":      {"Fri, 19 May 2017 07:52:17 GMT"}, 
    "Content-Geo-System":  {"wgs-84"}, 
    "Connection":    {"keep-alive"}, 
    "X-Frame-Options":   {"SAMEORIGIN"}, 
    "X-Xss-Protection":   {"1; mode=block"}, 
    "Server":     {"nginx"}, 
    "Content-Type":    {"application/json"}, 
} 
- RESPONSE BODY: 
&http.gzipReader{ 
body: &http.bodyEOFSignal{ 
    body: &http.body{ 
     src: &internal.chunkedReader{ 
      r: &bufio.Reader{ 
       buf: {0x48, 0x54, .......... } 

回答

0

現在我得到的響應標題"Content-Encoding": "gzip",雖然我仍然得到一個不可解讀的響應體,但這超出了這個問題的範圍。

+0

這可能是尤伯杯的API是足夠聰明,只包括Content-Encoding頭,如果請求者接受gzip的,但實際上沒有足夠聰明*不gzip壓縮的響應*當他們不接受的gzip。如果是這樣的話,這對於優步來說絕對是一個缺陷。 – Adrian

+0

但是在同一時間,他們固執地給我gzip-ped迴應,不管我是否要求他們 –