@@ -171,6 +171,7 @@ export default class HTTPClient {
171171 private static prepareResponse (
172172 res : BaseHTTPClientResponse ,
173173 format : 'application/msgpack' | 'application/json' ,
174+ parseBody : boolean ,
174175 jsonOptions : utils . JSONOptions = { }
175176 ) : HTTPClientResponse {
176177 let { body } = res ;
@@ -180,7 +181,7 @@ export default class HTTPClient {
180181 text = ( body && Buffer . from ( body ) . toString ( ) ) || '' ;
181182 }
182183
183- if ( format === 'application/json' ) {
184+ if ( parseBody && format === 'application/json' ) {
184185 body = HTTPClient . parseJSON ( text , res . status , jsonOptions ) ;
185186 }
186187
@@ -203,7 +204,8 @@ export default class HTTPClient {
203204 // eslint-disable-next-line no-param-reassign
204205 err . response = HTTPClient . prepareResponse (
205206 err . response ,
206- 'application/json'
207+ 'application/json' ,
208+ true
207209 ) ;
208210 // eslint-disable-next-line no-param-reassign
209211 err . status = err . response . status ;
@@ -218,13 +220,16 @@ export default class HTTPClient {
218220 * @param requestHeaders - An object containing additional request headers to use.
219221 * @param jsonOptions - Options object to use to decode JSON responses. See
220222 * utils.parseJSON for the options available.
223+ * @param parseBody - An optional boolean indicating whether the response body should be parsed
224+ * or not.
221225 * @returns Response object.
222226 */
223227 async get (
224228 relativePath : string ,
225229 query ?: Query < any > ,
226230 requestHeaders : Record < string , string > = { } ,
227- jsonOptions : utils . JSONOptions = { }
231+ jsonOptions : utils . JSONOptions = { } ,
232+ parseBody : boolean = true
228233 ) : Promise < HTTPClientResponse > {
229234 const format = getAcceptFormat ( query ) ;
230235 const fullHeaders = { ...requestHeaders , accept : format } ;
@@ -236,7 +241,7 @@ export default class HTTPClient {
236241 fullHeaders
237242 ) ;
238243
239- return HTTPClient . prepareResponse ( res , format , jsonOptions ) ;
244+ return HTTPClient . prepareResponse ( res , format , parseBody , jsonOptions ) ;
240245 } catch ( err ) {
241246 throw HTTPClient . prepareResponseError ( err ) ;
242247 }
@@ -251,7 +256,8 @@ export default class HTTPClient {
251256 relativePath : string ,
252257 data : any ,
253258 requestHeaders : Record < string , string > = { } ,
254- query ?: Query < any >
259+ query ?: Query < any > ,
260+ parseBody : boolean = true
255261 ) : Promise < HTTPClientResponse > {
256262 const fullHeaders = {
257263 'content-type' : 'application/json' ,
@@ -266,7 +272,7 @@ export default class HTTPClient {
266272 fullHeaders
267273 ) ;
268274
269- return HTTPClient . prepareResponse ( res , 'application/json' ) ;
275+ return HTTPClient . prepareResponse ( res , 'application/json' , parseBody ) ;
270276 } catch ( err ) {
271277 throw HTTPClient . prepareResponseError ( err ) ;
272278 }
@@ -280,7 +286,8 @@ export default class HTTPClient {
280286 async delete (
281287 relativePath : string ,
282288 data : any ,
283- requestHeaders : Record < string , string > = { }
289+ requestHeaders : Record < string , string > = { } ,
290+ parseBody : boolean = true
284291 ) {
285292 const fullHeaders = {
286293 'content-type' : 'application/json' ,
@@ -294,6 +301,6 @@ export default class HTTPClient {
294301 fullHeaders
295302 ) ;
296303
297- return HTTPClient . prepareResponse ( res , 'application/json' ) ;
304+ return HTTPClient . prepareResponse ( res , 'application/json' , parseBody ) ;
298305 }
299306}
0 commit comments