@@ -16,12 +16,17 @@ import (
1616
1717 "github.com/qiniu/go-sdk/v7/auth"
1818 "github.com/qiniu/go-sdk/v7/conf"
19+ internal_io "github.com/qiniu/go-sdk/v7/internal/io"
1920 "github.com/qiniu/go-sdk/v7/internal/log"
2021 "github.com/qiniu/go-sdk/v7/reqid"
2122)
2223
2324var UserAgent = getUserAgentWithAppName ("default" )
24- var DefaultClient = Client {& http.Client {Transport : http .DefaultTransport }}
25+ var DefaultClient = Client {
26+ & http.Client {
27+ Transport : http .DefaultTransport ,
28+ },
29+ }
2530
2631// 用来打印调试信息
2732var DebugMode = false
@@ -144,7 +149,7 @@ func (r Client) DoRequestWithForm(ctx context.Context, method, reqUrl string, he
144149 if headers == nil {
145150 headers = http.Header {}
146151 }
147- headers .Add ("Content-Type" , "application/x-www-form-urlencoded" )
152+ headers .Set ("Content-Type" , conf . CONTENT_TYPE_FORM )
148153
149154 requestData := url .Values (data ).Encode ()
150155 if method == "GET" || method == "HEAD" || method == "DELETE" {
@@ -170,7 +175,7 @@ func (r Client) DoRequestWithJson(ctx context.Context, method, reqUrl string, he
170175 if headers == nil {
171176 headers = http.Header {}
172177 }
173- headers .Add ("Content-Type" , "application/json" )
178+ headers .Set ("Content-Type" , conf . CONTENT_TYPE_JSON )
174179 return r .DoRequestWith (ctx , method , reqUrl , headers , bytes .NewReader (reqBody ), len (reqBody ))
175180}
176181
@@ -194,11 +199,12 @@ func (r Client) Do(ctx context.Context, req *http.Request) (resp *http.Response,
194199// --------------------------------------------------------------------
195200
196201type ErrorInfo struct {
197- Err string `json:"error,omitempty"`
198- Key string `json:"key,omitempty"`
199- Reqid string `json:"reqid,omitempty"`
200- Errno int `json:"errno,omitempty"`
201- Code int `json:"code"`
202+ Err string `json:"error,omitempty"`
203+ ErrorCode string `json:"error_code,omitempty"`
204+ Key string `json:"key,omitempty"`
205+ Reqid string `json:"reqid,omitempty"`
206+ Errno int `json:"errno,omitempty"`
207+ Code int `json:"code"`
202208}
203209
204210func (r * ErrorInfo ) ErrorDetail () string {
@@ -226,20 +232,21 @@ func (r *ErrorInfo) HttpCode() int {
226232
227233func parseError (e * ErrorInfo , r io.Reader ) {
228234
229- body , err1 := ioutil .ReadAll (r )
235+ body , err1 := internal_io .ReadAll (r )
230236 if err1 != nil {
231237 e .Err = err1 .Error ()
232238 return
233239 }
234240
235241 var ret struct {
236- Err string `json:"error"`
237- Key string `json:"key"`
238- Errno int `json:"errno"`
242+ Err string `json:"error"`
243+ Key string `json:"key"`
244+ Errno int `json:"errno"`
245+ ErrorCode string `json:"error_code,omitempty"`
239246 }
240247 if decodeJsonFromData (body , & ret ) == nil && ret .Err != "" {
241248 // qiniu error msg style returns here
242- e .Err , e .Key , e .Errno = ret .Err , ret .Key , ret .Errno
249+ e .Err , e .Key , e .Errno , e . ErrorCode = ret .Err , ret .Key , ret .Errno , ret . ErrorCode
243250 return
244251 }
245252 e .Err = string (body )
@@ -264,7 +271,7 @@ func ResponseError(resp *http.Response) error {
264271 if ok && strings .HasPrefix (ct [0 ], "application/json" ) {
265272 parseError (e , resp .Body )
266273 } else {
267- bs , err := ioutil .ReadAll (resp .Body )
274+ bs , err := internal_io .ReadAll (resp .Body )
268275 if err != nil {
269276 e .Err = fmt .Sprintf ("failed to read from response body: %s" , err )
270277 } else {
@@ -279,7 +286,7 @@ func ResponseError(resp *http.Response) error {
279286func CallRet (ctx context.Context , ret interface {}, resp * http.Response ) (err error ) {
280287
281288 defer func () {
282- io . Copy ( ioutil . Discard , resp .Body )
289+ _ = internal_io . SinkAll ( resp .Body )
283290 resp .Body .Close ()
284291 }()
285292
0 commit comments