Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Setting XMLHttpRequest.withCredentials=true on requests made with UnityWebRequest

Discussion in 'WebGL' 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:
    34
    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.