Comments (5)
@wrongsahil any ideas on this? Async fetch works no problem but I can't get async xhr to work properly.
from requestly.
Hey @anlinguist. Sorry for the delay in response. Currently, we only support async code in requests triggered using fetch. XHR isnβt supported at the moment.
We're discussing and testing a potential solution to enable support for XHR as well. We'll keep you posted on any developments.
from requestly.
hey, thanks for getting back to me @wrongsahil. For my purposes, I was able to proxy and monkey patch XHR to allow async operations on the XHR response data.
In short, my approach pauses the original XHR request before it is sent, creates and runs a fetch request from it, takes the fetch request response and freely applies async operations on it, resumes the original XHR request, and overrides the XHR response using the now available response from the fetch operation. Here's how I did it:
- Save a reference to the original XMLHttpRequest object and its methods (send, open, setRequestHeader).
- Override the global XMLHttpRequest object with a custom constructor function.
- Inside the custom constructor, capture the request details (URL, method, headers) by overriding the open and setRequestHeader methods and storing them in a requestDetails object.
- Override the send method to defer sending the request. It attempts to retrieve a response rule based on the request URL, request data, and method.
- If a response rule is found, it enters an async logic; otherwise, it sends the request as-is.
- In the async logic, a separate fetch request is made to the original URL using the captured request details. The response is then modified based on the response rule's modification function, if specified.
- The modified response is stored in a responseOverrides object, and getters are defined for the response, responseText, status, and statusText properties of the xhr object to return the modified response.
- Finally, the original send method is called to resume the original XHR request, but with the overridden response properties returning the modified response to the caller.
Of course, this approach does make an extra request to the original URL in order to fetch and modify the response data. This additional request may have performance implications, especially if the response modifications are extensive or if there are a large number of XHR requests being intercepted. For my purposes, this is acceptable.
from requestly.
@anlinguist If API request is not idempotent in nature, firing it multiple times may lead to unexpected results. For instance, a POST request which creates new data on server may create it multiple times.
from requestly.
@lazyvab Yes, that's true, so for many usecases, it may not be an ideal solution. If the request you're targeting is an XHR and ends up creating/updating/deleting data on a server, this would not be an ideal approach.
However, if the request you're targeting is for obtaining data from a server, it works. For my purposes, this was an acceptable workaround - I have rules around which XHR requests duplicate the request using the fetch request approach, and all of my rules really focus on getting data from the server rather than posting/putting/deleting/etc.
Due to the synchronous nature of XHR's readystatechange, I could not figure out another way to perform asynchronous logic directly on the XHR request.
from requestly.
Related Issues (20)
- Add breakpoints and modify the whole request using JS HOT 1
- Add Support for variables
- Error when creating a new rule HOT 1
- Add support for attributes while injecting scripts in the Insert Scripts Rule HOT 2
- bug: LINK.REQUESTLY_DOCS_TEST_URL_CONDITION outdated HOT 2
- Modifying HTTP Status Code HOT 1
- bug: Loading huge rule collection start to failed HOT 2
- [Features Request] API Client Module HOT 1
- bug: Not visible in Browser DevTools message should not appear in Desktop App
- bug: Filter doesn't get cleared when navigated from Network Traffic to View Session
- bug: Request header inspection doesn't work for `Accept-Encoding`. [Mac Desktop] HOT 2
- bug: Windows x64 Chrome (122.0.6261.112) does not work
- Import yaml & json files with OpenAPI specs in Mock Server
- bug: ENGG-1447: a bug occurs when I type a long string in the regex test for the API Response mock HOT 2
- bug: Session records not work for Chrome extensions HOT 2
- Improve Response Viewer in Desktop App Traffic Table (From Reddit)
- feat: Self Hosting Support
- How To Use SessionBook/Recording Feature To Capture All Requests (Documents, Images, Audio, Video) HOT 3
- Add Support for Folders/Collections for API Mocks in Mock Server
- Make it easier to test "Overriding API Responses" feature HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from requestly.