2017-05-19 3 views
1

net/http 라이브러리를 '이동'에 사용하여 HTTP GET 요청을합니다. 응답에서, 나는 12의 우두머리를 얻는다. 그러나 우편 발송자를 통해 정확히 동일한 쿼리를 실행하면 16 개의 헤더가 생성됩니다. 그 중 하나는 'Content-Encoding'입니다. 이 질문은 CORS 문제 여야합니다.Go : 응답을 수동으로 압축 풀기 위해 gzip 인코딩을 감지했지만 'Content-Encoding'헤더가 누락되었습니다.

하지만 내 요청에 Accept-Encoding: gzip 헤더를 설정하지 않았기 때문에 응답으로 gzip 인코딩을 받고 있는데 이동 전송은 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'입니다 :

여기에 내가이 일을하려고 내 코드입니다.

- 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

I는 위버 API의 완고함에 준하고 다른 요청 헤더, req.Header.Add("Accept-Encoding", "gzip") 추가 : 여기서 출력 (응답)이다.

응답 헤더 "Content-Encoding": "gzip"이 나옵니다. 아직 해독 할 수없는 응답 본문이 있지만이 질문의 범위를 벗어납니다.

+0

리퀘 스터가 gzip을 수락하지만 gzip을 수락하지 않는 경우 응답 *을 gzip하지 않을 정도로 충분히 영리하지 않은 경우에만 Uber의 API가 Content-Encoding 헤더를 포함 할만큼 충분히 똑똑 할 수 있습니다. 우버의 종말이 사실이라면 이것은 확실히 결함이 될 것입니다. – Adrian

+0

그러나 동시에, 그들은 나에게 gzip-ped 응답을 줄만큼 충분히 완고합니다. –