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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityWebRequest - error: 415 Unsupported Media Type

Discussion in 'Scripting' started by hromoyDron, Dec 30, 2019.

  1. hromoyDron

    hromoyDron

    Joined:
    Jan 24, 2013
    Posts:
    90
    Hello.

    I'm preparing my game for publishing on ArmorGames.com
    I use their API to get the id of the current user:
    https://services.armorgames.com/services/rest/v1/authenticate/user.json

    When I click on this link in the browser, I get the correct answer.
    When sending a GET request through my build, I get an error: HTTP / 1.1 415 Unsupported Media Type

    Code (CSharp):
    1. IEnumerator GetText (Action <string> callback)
    2. {
    3.  
    4.         string url;
    5.         url = "https://services.armorgames.com/services/rest/v1/authenticate/user.json";
    6.  
    7.         UnityWebRequest www = UnityWebRequest.Get (url);
    8.         www.SetRequestHeader ("Content-Type", "application/json");
    9.      
    10.         yield return www.SendWebRequest ();
    11.  
    12.         if (www.isNetworkError || www.isHttpError) {
    13.             Debug.Log ("MyError:" + www.error);
    14.             callback (null);
    15.         }
    16.         else {
    17.          
    18.                 Debug.Log ("resp:" + www.downloadHandler.text);
    19.                 callback (www.downloadHandler.text);
    20.         }
    21.     }
    What could be the problem?
    I tried both with the addition of "Content-Type", "application/json", and without. The result is the same.
     
    Last edited: Dec 30, 2019
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Your code seems to have some spaces in it that shouldn't be there. For example, if you got to the web address:

    Code (CSharp):
    1. https://services.armorgames.com/services/rest/v1/authenticate/user. json
    you will get the error you mentioned.

    If you remove the space between user. and json it works ok

    Code (CSharp):
    1. https://services.armorgames.com/services/rest/v1/authenticate/user.json
    p.s. 'application / json' shouldn't have spaces either - try it with 'application/json'
     
    bugfinders likes this.
  3. hromoyDron

    hromoyDron

    Joined:
    Jan 24, 2013
    Posts:
    90
    It's copy/past spaces. I don't have them in my code. There should be another problem.
     
    Last edited: Dec 31, 2019
  4. hromoyDron

    hromoyDron

    Joined:
    Jan 24, 2013
    Posts:
    90
    Sorry. You are right.
    I thought that forum layout make this spaces.
    But there is invisible space in VScode.
    Thank you. Now all works fine!
     
    tonemcbride likes this.