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

Resolved Getting 415 / 400 when querying test allocation via REST api

Discussion in 'Game Server Hosting' started by Selmar, Jun 25, 2023.

  1. Selmar


    Sep 13, 2011

    I've been trying to create a test allocation for the last few days and I'm stuck now, on two seemingly independent problems.

    1. When using my browser to make the request, I get a status 400. I think I got past the authentication, because I am getting access denied if I change something in the request header (initially I assumed capitalization doesn't matter but it seems the authorization header value has to have a capital letter for Basic). The response I get now is:
    The body of the request is as follows. I generated the UUID for the allocation myself (my understanding is that's what I'm supposed to do). I currently have only one build, build config, and fleet. I got the build configuration id from the fleet's build configurations, and the region id from the scaling settings.

    2. When using UnityWebRequest, I keep getting a 415 Invalid Media Type, but I don't find anything that is wrongly configured (and I'm using the same links and headers for the request via the browser). This problem is the reason I started trying via the browser before trying to resolve it, because I thought perhaps I'm using the API wrong somehow.

    The code for this:

    Code (CSharp):
    1.     static async Task<AllocationRequestResponse> _QueueTestAllocationRequest(InitGameSessionServerData initServerData)
    2.     {
    3.         AllocationRequest payload;
    4.         payload.allocationId = Guid.NewGuid().ToString();
    5.         payload.buildConfigurationId = _buildConfigurationId;
    6.         payload.payload = JsonUtility.ToJson(initServerData);
    7.         payload.regionId = _regionId;
    8.         payload.restart = false;
    9.         string jsonPayload = JsonUtility.ToJson(payload);
    11.         string path = _multiplayTestAllocationPath;
    12.         string headerName = "Authorization";
    13.         string headerValue = $"Basic {_keyIdAndSecret}";
    14.         var tokenReq = new UnityWebRequest(path, UnityWebRequest.kHttpVerbPOST);
    15.         tokenReq.SetRequestHeader(headerName, headerValue);
    16.         tokenReq.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonPayload));
    18.         var tokenOp = tokenReq.SendWebRequest();
    19.         while (!tokenOp.isDone) await Task.Delay(100);
    21.         if (tokenReq.responseCode != 202)
    22.             Debug.LogError($"Queue test allocation request failed '{tokenReq.responseCode}': '{tokenReq.error}'");
    23.         if (tokenReq.downloadHandler != null)
    24.             return JsonUtility.FromJson<AllocationRequestResponse>(tokenReq.downloadHandler.text);
    25.         return default;
    26.     }
    Of course, I double-checked all the links and IDs, and I even wrote them from scratch.
    Any ideas are welcome!
    Last edited: Jun 26, 2023
  2. Selmar


    Sep 13, 2011
    Turns out I was missing a
    header specifying
    .. The web newbie mistake I suppose!