title: Make a GET request to localhost in Unity C# script
body: <p>I'm trying to make a GET request for a JSON payload from localhost in a Unity C# script. I've made successful POST, PUT, and GET requests with a separate C# file, but unity requires use of UnityWebRequest(s). My Unity code returns an error of "Unknown Error" in the Unity console, but in the log files the first error is cannot connect to destination host".</p>
  
  <p>Similar questions were resolved by changing the web address. I know that my web address is correct, as I can make successful requests with the Postman application and my own separate C# code.
  The debug code returns True for isNetworkError.</p>
  
  <p>I set up my server using the ToDo API tutorial <a href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&amp;tabs=visual-studio" rel="nofollow noreferrer">here</a> and modified the item contents to just contain the ID and string data.</p>
  
  <p>If somebody could tell me why it doesn't work, and provide a solution, I'd be very grateful!</p>
  
  <p>My code:</p>
  
  <pre><code>    IEnumerator GetText()
  {
      using (UnityWebRequest www = UnityWebRequest.Get("http://...localhost:[my port]/api/Todo"))
      {
          yield return www.SendWebRequest();
  
          if (www.isNetworkError || www.isHttpError)
          {
              UnityEngine.Debug.Log(www.error);
              UnityEngine.Debug.Log(www.isNetworkError);
          }
          else
          {
              // Show results as text
              UnityEngine.Debug.Log(www.downloadHandler.text);
          }
      }
  }
  </code></pre>
  
  <p>This is the format of the data on the web server:</p>
  
  <pre><code>[
    {
      "id": 2,
      "data": "String text"
    }
  ]
  </code></pre>
  
  <p>For reference, here is part of the working C# code that works separately to do the same thing:</p>
  
  <pre><code>async static void GetRequest(string url)
  {
      using (HttpClient client = new HttpClient())
      {
          using (HttpResponseMessage response = await client.GetAsync(url))
          {
              using (HttpContent content = response.Content)
              {
                  string mycontent = await content.ReadAsStringAsync();
  
                  UnityEngine.Debug.Log(content);
              }
          }
      }
  }
  </code></pre>