0

I have the following c# post request code:

public static string getJSON()
var client = new RestClient("");
          var request = new RestRequest(Method.POST);
          var httpWebRequest = (HttpWebRequest)WebRequest.Create("url") 
          request.AddHeader("cache-control", "no-cache");
          request.AddParameter("undefined", "{\r\n\t\"application\": {\r\n\t\t\"id\":3\r\n\t},\r\n\t\"luminaire\": {\r\n\t\t\"id\":60\r\n\t},\r\n\t\"room\": {\r\n\t\t\"length\":" + roomSize.x + ",\r\n\t\t\"width\":" + roomSize.z+ ",\r\n\t\t\"height\":"+roomSize.y+",\r\n\t\t\"discretizedPitch\":{\r\n\t\t\t\"x\":0.6,\r\n\t\t\t\"y\":0.6\r\n\t\t}\r\n\t}\r\n}", ParameterType.RequestBody);
          IRestResponse response = client.Execute(request);
          return response.Content;

Now this works in normal unity, however when I make a webbuild this encounters null related problems.

I also tried rewriting the code into unitynetwork code (using Sending http requests in C# with Unity)

    public static void getJSON(){

        var s = StartCoroutine(PostRequest("url", "\"application\": {\"id\":3},\"luminaire\": {\"id\":60},\"room\": {\"length\":" + roomSize.x + ",\"width\":" + roomSize.z + ",\"height\":" + roomSize.y + ",\"discretizedPitch\":{\"x\":0.6,\"y\":0.6}}"));

    }

    string res = "";

    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);
            res = "error";
        }
        else
        {
            res = uwr.downloadHandler.text;
            Debug.Log("Received: " + uwr.downloadHandler.text);
        }
    }

however this just throws a server 500 error rather than anything useful, anybody know how to either port the first code into unitynetwork code or why the first code gives a null exception in webgl (but not in normal code)?

Thijser
  • 2,625
  • 1
  • 36
  • 71
  • Don't use Thread.Sleep on the main Thread. You can use Action to return the result from a coroutine function or make the call from another coroutine function then yield it. – Programmer Oct 14 '18 at 19:38
  • @Programmer alright, thanks. Btw I used the linked answer as a basis for my unity code (the button one) so it's not really a duplicate as it doesn't work for me (might be because of the nested properties?). – Thijser Oct 14 '18 at 19:40
  • Re-opened the question. Fix your current code to make sure that's not the issue. – Programmer Oct 14 '18 at 19:41
  • @Programmer, alright I will look into how to wait till the coroutine is finished. – Thijser Oct 14 '18 at 19:43
  • See [this](https://stackoverflow.com/a/52712676/3785314) for example on how to use Action to accomplish this – Programmer Oct 14 '18 at 19:43
  • Can you add the agent header and see if that's the issue? Maybe your server is checking for that? Something like this: `uwr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")` – Programmer Oct 14 '18 at 19:52
  • @Programmer OK now I feel stupid, turns out I was simply missing a pair of brackets in the json. I guess the lesson here is check your JSON? – Thijser Oct 14 '18 at 20:06

1 Answers1

1

OK now I feel stupid,

the json should of course have been

"{\"application\": {\"id\":3},\"luminaire\": {\"id\":60},\"room\": {\"length\":" + roomSize.x + ",\"width\":" + roomSize.z + ",\"height\":" + roomSize.y + ",\"discretizedPitch\":{\"x\":0.6,\"y\":0.6}}}"

in order to be the same, a simple copy paste error.

Thijser
  • 2,625
  • 1
  • 36
  • 71