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. Dismiss Notice

Question How to proper use UnityWebRequest.Post

Discussion in 'Scripting' started by Rachan, Jun 9, 2023.

  1. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    654
    Hi there!

    I using Unity 2021.3.16f1 LTS
    and I would like to use UnityWebRequest.Post properly,
    actually I used to following an examples from internet,
    But a data that had Post still blank at field, so maybe I do a wrong C# code or something,
    So I want to make it to properly,

    anyone has any suggestion?

    Thanks!!!
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,874
    Rachan likes this.
  3. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    654
    Thanks for the answer, But the code from document that you gave me is still not work...

    here is my code
    Code (CSharp):
    1.  
    2. string url = "https://www.hardworkerstudio.com/zombiedozen/post.php?rand="+Random.Range(0,1000);
    3.  
    4. WWWForm form = new WWWForm();
    5. form.AddField("data", "You should see this data back in Log");
    6.  
    7. using (UnityWebRequest www = UnityWebRequest.Post(url, form))
    8. {
    9.      yield return www.SendWebRequest();
    10.      if (www.result != UnityWebRequest.Result.Success)
    11.      {
    12.           Debug.Log(www.error);
    13.      }
    14.      else
    15.      {
    16.           Debug.Log(www.downloadHandler.text);
    17.      }
    18. }
    19.  
    You can try to send to my website, if it work it will show this message in log

    "You should see this data back in Log"

    and here is post.php script
    Code (JavaScript):
    1. <?php
    2.  
    3.     $data = $_POST["data"];
    4.     echo ">".$data."<";
    5.  
    6. ?>
    I used "> <" because, I want to see it's actual data from my php file, not just a blank.

    Thanks!!!
     
    Last edited: Jun 10, 2023