- Home /
Date request header is missing from http request(UnityWebrequest)
I am trying to send a GET request to an API that requires the Date request header.
However UnityWebRequest does not send it automatically (I have checked with https://requestb.in/) and I cannot set it manually since "date" is one of the protected headers.
I am able to communicate with the API successfully if I use the WWW class (which does allow me to set the "date" header manually) but am trying to do the same with the UnityWebRequest since WWW will be deprecated eventually.
So, how can I make sure that the "date" request header is sent?
Is there a solution or do I have to use an object from a different library such as .NET's HttpWebRequest?
Thanks
Uhm, you should have included the code that you're using and tell us for whivh platform you build for. I was never in need to set this header, however i can confirm (by looking at the UnityWebRequest source code) that "date" is on the list of forbidden header keys. Depending on your platform you might be able to use reflection to directly use "InternalSetRequestHeader" to bypass all checks. This of course is a bit dangerous.
Sometimes i'm not sure what UT though when they made certain decisions ^^. In some environments it makes sense to restrict certain headers to avoid abuse, but for example in standalone builds those restrictions make not much sense to me.
Though the UnityWebRequest is still in the "Experimental" namespace AFAI$$anonymous$$.
The relevant bit of the code is the following:
UnityWebRequest www = UnityWebRequest.Get (fullURL);
www.SetRequestHeader("Date", dateTimeString);
yield return www.Send();
And the error message I get is: ArgumentException: Cannot set Request Header Date - name contains illegal characters or is not user-overridable
I am building to IOS and Android.
You are right about the protected list but not only can I not change it (since it is protected) but Unity does not send the "date" header at all. Other protected headers fields such as "host" and "accept-encoding" cannot be modified either but at least they are sent along with the request.
I have tried to use System.Reflection as you suggested, as follows:
System.Reflection.$$anonymous$$ethodInfo dyn$$anonymous$$ethod = myReq.GetType ().Get$$anonymous$$ethod ("InternalSetRequestHeader", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
dyn$$anonymous$$ethod.Invoke (myReq, new object[]{"Date", dateTimeString });
but I get the following error:
InvalidOperationException: Cannot override system-specified headers
Too bad. Well in this case they also implemented another check on the native side. So that simply renders the UnityWebRequest class useless for your case, end of story ^^. You might want to use the .NET classes ins$$anonymous$$d.
The HttpWebRequest is not that difficult to use, the only problem is that it's a blocking request, so you would need to use a thread and implement some mechanics to get the result back into the main thread once finished. If you only need a few requests you can wrap all this in a seperate class that is derived from CustomYieldInstruction. That way you can still use it in a coroutine.
If you need to do a lot requests frequently you might want to use a threadpool or a producer - consumer pattern with a permanent worker thread.
Answer by josefgrunig · Feb 20, 2018 at 03:54 PM
There are multiple issues here. First UnityWebRequest is not sending the "Date" header and secondly you are not allowed (since Unity 2017.1) to set it manually anymore. I found a workaround setting the lower case "date" header instead of "Date". Its terrible I know, but there are service like Vuforia Cloud API that require the "date" header and must be set from client side because used in the signature: https://library.vuforia.com/articles/Training/Using-the-VWS-API
Your answer
Follow this Question
Related Questions
How do I add a header and a file at the web request at the same time? 0 Answers
Posting raw XML data to web - no parameter name 2 Answers
WWW Windows Phone 8 / Windows RT 8.1 missing first line http status header 0 Answers
Is System.Net.HttpWebRequest supported on Android/iOS 1 Answer
Is HttpWebRequest supposed to work in Web Builds or not? 1 Answer