1

After getting this demo server working I am able return GET requests from it to Unity, but when I would try to send data from Unity to the local server using POST requests it would only show null values added into the server. This is the code I was using in Unity:

IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("charge","+4/3");
    form.AddField("name", "doubletop");

    using (UnityWebRequest www = UnityWebRequest.Post("http://localhost:5000/quarks/", form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}

I would get "Form upload complete!" in the console, and GET requests would work, but those null values kept coming.

sundialer
  • 33
  • 8
  • 2
    Please don't edit the question to include the answer. the answer should be written as an answer below. See [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – Ruzihm Dec 30 '19 at 04:05
  • 1
    Okay, just fixed it. Thanks – sundialer Dec 30 '19 at 18:23

1 Answers1

2

I modified my Upload() method to the PostRequest() in this example, and now it works!

Here's the full code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class HTTP : MonoBehaviour
{
    void Start()
    {
        // A correct website page.
        StartCoroutine(GetRequest("localhost:5000/quarks"));
        PostData();
        StartCoroutine(GetRequest("localhost:5000/quarks"));

        // A non-existing page.
        //StartCoroutine(GetRequest("https://error.html"));
    }

    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string[] pages = uri.Split('/');
            int page = pages.Length - 1;

            if (webRequest.isNetworkError)
            {
                Debug.Log(pages[page] + ": Error: " + webRequest.error);
            }
            else
            {
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
        }
    }

    [Serializable]
    public class Quark
    {
        public string name;
        public string charge;
    }

    public void PostData()
    {

        Quark gamer = new Quark();
        gamer.name = "doublebottom";
        gamer.charge = "4/3";

        string json = JsonUtility.ToJson(gamer);
        StartCoroutine(PostRequest("http://localhost:5000/quarks", json));
    }

    IEnumerator PostRequest(string url, string json)
    {
        var uwr = new UnityWebRequest(url, "POST");
        byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
        uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
        uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        uwr.SetRequestHeader("Content-Type", "application/json");

        //Send the request then wait here until it returns
        yield return uwr.SendWebRequest();

        if (uwr.isNetworkError)
        {
            Debug.Log("Error While Sending: " + uwr.error);
        }
        else
        {
            Debug.Log("Received: " + uwr.downloadHandler.text);
        }
    }
}
sundialer
  • 33
  • 8