Zgragselus
Zgragselus

Reputation: 19

Post request in Unity

I'm trying to do a POST request from Unity3D to my web server. Everything works correctly in editor, but when I build the binaries and try to do the same from them - it doesn't look like it reaches server.

On server I process them as:

if(isset($_POST['name']) || isset($_POST['score']))
{
    $name = $_POST['name'];
    $score = $_POST['score'];
    ....
}

And in Unity/C# I do this:

IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("name", name.text.Replace("|", " ").ToString());
    form.AddField("score", (int)(highScore));
    UnityWebRequest www = UnityWebRequest.Post("https://server_name.com/page.php", form);
    yield return www.Send();
}

Is there any reason why this should work in editor and shouldn't work while running built binaries?

Thanks!

EDIT: As per request - my binaries were built for Windows and Linux, and both have the same error. I'm running editor on Windows (same machine as on which I used Windows binary) - and I don't have any problem in editor.

Upvotes: 0

Views: 1062

Answers (1)

FLX
FLX

Reputation: 2679

I don't even understand how this is working inside the editor...

You are not following the way described in the doc :

https://docs.unity3d.com/ScriptReference/WWWForm.html

You don't have to use UnityWebRequest at all: only WWW and WWWForm.

Try this :

IEnumerator Upload () {
    WWWForm form = new WWWForm();
    form.AddField("name", name.text.Replace("|", " ").ToString());
    form.AddField("score", (int)(highScore));

    WWW result = new WWW("https://server_name.com/page.php", form );

    yield return result;

    if(!string.IsNullOrEmpty(result.error)) {
        print( "Error : " + result.error );
    } else {
        Debug.Log(result.text);
    }
}

Edit :

Ok my bad, your example is on the doc. I wasn't aware that WWW was evolving (Nice communication unity...).

So here it my advice : Keep using legacy WWW. It's working perfectly fine.

Because this is classic unity : When they say a new feature is out, it's broken.

Upvotes: 1

Related Questions