mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-17 01:17:43 +00:00
Add support for the gzipped response.
This commit is contained in:
+15
-1
@@ -2,6 +2,7 @@ package gql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -57,7 +58,20 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
|||||||
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
|
return fmt.Errorf("HTTP error - unacceptable status code: \"%s\" for \"%s\"", httpResponse.Status, httpRequest.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(httpResponse.Body)
|
var reader io.ReadCloser
|
||||||
|
encoding := httpResponse.Header.Get("Content-Encoding")
|
||||||
|
if encoding == "gzip" {
|
||||||
|
reader, err = gzip.NewReader(httpResponse.Body)
|
||||||
|
if err != nil {
|
||||||
|
qe.Logger.Debug("Error while creating gzip reader;", map[string]interface{}{"error": err.Error()})
|
||||||
|
return fmt.Errorf("Error while creating gzip reader: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
} else {
|
||||||
|
reader = httpResponse.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()})
|
qe.Logger.Debug("Error while reading http response;", map[string]interface{}{"error": err.Error()})
|
||||||
return fmt.Errorf("Error while reading http response: %s", err.Error())
|
return fmt.Errorf("Error while reading http response: %s", err.Error())
|
||||||
|
|||||||
Reference in New Issue
Block a user