Search Unity

Setting XMLHttpRequest.withCredentials=true on requests made with UnityWebRequest

Discussion in 'Web' started by ndesy, Dec 19, 2019.

  1. ndesy

    ndesy

    Joined:
    Jul 22, 2019
    Posts:
    20
    Hello,

    I'm using Unity 2019.2.15f and I have a cookies issue when testing the final webAssembly. The backend is splitted in multiple subdomains and the session cookies are not sent when sending a request using UnityWebRequest.

    I'm looking for a way to manually assign properties to the XmlHttpRequest instance used to send the request to the server.

    I'm confident that adding this line would fix my issue :

    Code (JavaScript):
    1. request.withCredentials = true;

    However, the only examples I can find are old and not compatible with WebAssembly export format.

    Someone has an idea?

    Thanks in advance!
     
  2. Mohelm97

    Mohelm97

    Joined:
    Feb 6, 2013
    Posts:
    11
    Unity uses XMLHttpRequest, you could override the open function in JS like this:
    Code (JavaScript):
    1. XMLHttpRequest.prototype.originalOpen = XMLHttpRequest.prototype.originalOpen || XMLHttpRequest.prototype.open;
    2. XMLHttpRequest.prototype.open = function (...args) {
    3.     this._url = args[1];
    4.     if (this._url.indexOf("API_URL") != -1)
    5.         this.withCredentials = true;
    6.     this.originalOpen(...args);
    7. }
    Just replace API_URL with your API Url.
    Note: put this script anywhere in the WebGL page.
     
  3. Guillaume-atVolumiq

    Guillaume-atVolumiq

    Joined:
    Sep 14, 2018
    Posts:
    36
    Doesn't seem to work anymore, if I put a log in the fonction it does fire a few times when it start but no longer once the app is fully loaded and send requests
     
    Alex_torrus likes this.