You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage/http-client.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -300,7 +300,7 @@ const request = {
300
300
method:'GET',
301
301
responseInterceptor:res=> {
302
302
res.arbitraryProp='arbitrary value';
303
-
returnreq;
303
+
returnres;
304
304
},
305
305
};
306
306
@@ -396,6 +396,34 @@ SwaggerClient.http(request);
396
396
> *__Note:__ you can mutate or assign any property of `Response` object as long as your
397
397
interceptor produces a valid `Response` object again.*
398
398
399
+
###### Accessing request in Response Interceptor
400
+
401
+
In order to access original `Request` in `responseInterceptor` you have to declare
402
+
`responseInterceptor` either as [function declaration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function) or [function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function).
403
+
By doing so, the interceptor will be bound to the original `Request`.
404
+
405
+
```js
406
+
importSwaggerClientfrom'swagger-client';
407
+
408
+
constrequest= {
409
+
url:'https://httpbin.org/',
410
+
method:'GET',
411
+
responseInterceptor:function(res) {
412
+
constrequest=this;
413
+
console.log(request.url); // https://httpbin.org/
414
+
console.log(request.method); // GET
415
+
416
+
res.arbitraryProp='arbitrary value';
417
+
return res;
418
+
},
419
+
};
420
+
421
+
SwaggerClient.http(request);
422
+
```
423
+
424
+
> *__Note:__ this will not work if [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) are used to define a `responseInterceptor`.*
425
+
426
+
399
427
#### Response serialization
400
428
401
429
We detect the response `Content-Type` which identifies the payload of the `Response`.
0 commit comments