Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass request headers to native xhr request in xhrinterceptor #163

Open
zzarcon opened this issue Oct 17, 2019 · 0 comments
Open

Pass request headers to native xhr request in xhrinterceptor #163

zzarcon opened this issue Oct 17, 2019 · 0 comments

Comments

@zzarcon
Copy link
Member

zzarcon commented Oct 17, 2019

Currently, when there is no registered route handler we bypass the request but we are not passing the headers on the XHR case. That can be solved by setting the request headers into the real request:

// xhrinterceptor

nativeSend(data?: any): void {
    const request = new NativeXMLHttpRequest();

    request.timeout = this.timeout;
    request.onload = () => {
      const headers = request
        .getAllResponseHeaders()
        .split("\r\n")
        .reduce((previous, current) => {
          const [header, value] = current.split(": ");
          return {
            ...previous,
            [header]: value
          };
        }, {});
      const response = new KakapoResponse(
        request.status,
        request.responseBody,
        headers
      );
      this._handleResponse(response);
    };

    request.open(this._method, this._url);
    // start of the fix
   Object.keys(this._requestHeaders).forEach(headerName => request.setRequestHeader(headerName, this._requestHeaders[headerName]))
   // end of the fix
    request.send(data);
  }
}

cc @mtscrb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant