Hey everyone,
I’m facing a dilemma with UnityWebRequest
user-agent. As per the documentation, UnityWebRequest
sets the User-Agent
header itself.
What I’m observing is not at all what the documentation says: the header is always null
. Here is a sample code of what i’m doing right now:
using var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonPayload);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
This is what I ended up doing to set my user agent header manually:
request.SetRequestHeader("User-Agent", "Mozilla/5.0 ...");
But now I’m getting a warning from the engine in WebGL builds complaining about setting the user agent manually, while it is null…
I run my tests on WebGL, Standalone (Windows 10) and Android. Same behaviour. I’m using Unity 2021.3.
Am I missing something? Should I ignore the warning all-together?
Are you calling GetRequestHeader() to obtain it? That will return not return it, it is set by unity when sending request.
On WebGL you cannot set this header at all, it is automatically set by browser.
Yeah, I’m calling request.GetRequestHeader()
to find out what Unity’s doing. I was afraid that it might be too “early” and the header is set “on the fly” when sending the request but using the chrome debug tools on WebGL to inspect the request, the header is still null (or empty string, which doesn’t matter). Hence my question.
Note: I tried with/without manually setting the request headers. Same result: null user-agent.
You can send request to httpbin.org, it will send you back json containing the request it has received.
Ok, WebGL seems to set the User-Agent correctly after-all.
So, I should manually set my user-agent header on all platforms (including editor) except WebGL, right?
Unity will set it automatically everywhere, WebGL is special in that even Unity can’t set it there.
Ok, the User-Agent returned by httpbin seems fine: UnityPlayer/2021.3.43f1 (UnityWebRequest/1.0, libcurl/8.5.0-DEV)
.
Now I think my problem lies elsewhere: the server I’m making requests to (which I do not have access to) is applying some filtering based on the user-agent. The Unity UA seems to trigger the bot detection mechanism of said server.
From what I understand, It’s not recommended to set the header manually, but what if I need to “spoof” my UA to pass the bot detection filter?
Moreover, the server is an analytics server, who is sorting devices by the user agent. If the UA it always the same (which is what i’m observing at least on Standalone, Editor and Android) what can I do?