Hi,
trying to resolve this issue by myself, I notice something strange.
From the Unity manual, it appears that when I use the UnityWebRequest.Post() method, I must specify the data to send. However, I can also use the .uploadHandler = new UploadHandlerRaw() code to define the data to send.
Now, said that, I expected that the data parameter of the UnityWebRequest.Post() was optional and not mandatory, so I can choose if to send my data with the Post() method or with the UploadHandlerRaw() method.
So, as an attempt, I modified this line of code:
using (UnityWebRequest httpRequest = UnityWebRequest.Post(url, content))
with this:
using (UnityWebRequest httpRequest = UnityWebRequest.Post(url, string.empty))
and now everything seems to work and the “Native Collection…” error doesn’t appear!
UnityWebRequest.Post assigns an upload handler for you.
With the way you create web request, just do new UnityWebRequest() and pass relevant arguments to constructor.
Also, you use the using block, so you don’t need to Dispose() the request, using block does this automatically for you.
The string parameter in Post() assumes HTML form and does perform escaping. If your string is not HTML form, then escaping might be corrupting the string and your PHP server doesn’t accept it afterwards.
So, like I said: use constructor.
Hi,
ok, I’ve understood and I’m using the constructor. Everything works ok.
However, in my very personal opinion, I think that the online manual is not very clear on this. There are no clear examples of how to use the UnityWebRequest class. And the curious thing is that googling around I find a lot of people who are using this class in the wrong way!!! Maybe, on the UploadHandlerRaw() page, an example that shows how to send post data using the constructor would have been very useful.
Anyway, thank you for your support! Really appreciated!
string projRequest = sb.ToString(); // string is NOT HTML
string projectionUri = _useDevHeightTransformEndpoint ? TransformConstants.DevProjectionUri : TransformConstants.ProjectionUri;
// Leave string.empty so that it doeesn't create a second uploadHandler that causes a memory leak
using (UnityWebRequest client = UnityWebRequest.Post(projectionUri, string.Empty /*projRequest*/))
{
client.SetRequestHeader("Content-Type", "application/json");
client.SetRequestHeader("Accept", "application/json");
client.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(projRequest));
client.uploadHandler.contentType = "application/json";
yield return client.SendWebRequest();
So, the above doesn’t leak.
but this does…
using (UnityWebRequest client = UnityWebRequest.Post(projectionUri, projRequest))
and as suggested I’m “using the constructor”. I imagine that the projRequest get mangled above too - it’s not html, it’s json.
How do I use the constructor to send Encoding.UTF8.GetBytes(projRequest) and not use new UploadHandler(). using (UnityWebRequest client = UnityWebRequest.Post(projectionUri, Encoding.UTF8.GetBytes(projRequest)) doesn’t work either.
Which is interesting because there is a Put byte[ ] function but no Post one.
//
public static UnityWebRequest Put(string uri, byte[] bodyData);
Note: I didn’t write the code above - just trying to fix the memory leak properly.
Looks like projRequest is trying to send json - which I imagine the old author is changing to byte stream to avoid the html mangling.
Have you actually read any reply that was posted here?
It seems you don’t really understand what a constructor is.
Just to be more explicit here. The Post method is not a constructor. When you use the static Post method to create a webrequest, Unity creates an upload and download handler for you. However this upload handler will expect the string to represent an URL encoded HTML form string. So something like field=42&other=foobar&another=123. Such a string would be URL encoded. So providing any other string to the Post method would most likely corrupt the data as it is URL encoded.
So as it was mentioned several times, when you want to post raw data, do not use the static Post method. Instead just create a UnityWebRequest the usual way by using the constructor.
So instead of using
using (UnityWebRequest client = UnityWebRequest.Post(projectionUri, string.Empty /*projRequest*/))
use
using (UnityWebRequest client = new UnityWebRequest(projectionUri, "POST"))
In my case
When i close popup modal then root class was disabled after start coroutine, then i closed this popup model when finish the request and solved