Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Struggling to update RemoteConfig via REST API

Discussion in 'Unity Remote Config' started by NoshBar, Jan 16, 2024.

  1. NoshBar

    NoshBar

    Joined:
    Nov 19, 2012
    Posts:
    3
    Hi!

    We're trying to make a C# tool that updates the remote config periodically using the Admin REST API.

    We tried getting the existing configuration, changing a value, then pushing the altered value back up, but got an error about deserializing the data:
    MalformedRequestContentRejection: Cannot deserialize value of type `com.unity.remoteconfig.models.Config` from Array value (token `JsonToken.START_ARRAY`)

    So to make things simpler, we copied and pasted the example JSON from the Admin REST documentation page, and tried to use that, but got the exact same error.

    I get that this is less of a Unity issue and more of a JSON issue, but I don't understand what format to send the JSON up in, then, and any help would be appreciated!

    The code:
    Code (CSharp):
    1.  
    2. var jt = JToken.Parse("{\r\n  \t\"type\": \"settings\",\r\n  \t\"value\": []\r\n  }");
    3. var request = new HttpRequestMessage(HttpMethod.Put, url);
    4. request.Headers.Authorization = new AuthenticationHeaderValue("Basic", fullKey64);
    5. request.Content = JsonContent.Create(jt);
    6.  
    7. try
    8. {
    9.     using (HttpClient client = new HttpClient())
    10.     {  
    11.         using (HttpResponseMessage response = await client.SendAsync(request))
    12.         {
    13.             using (HttpContent content = response.Content)
    14.             {
    15.                 var data = await response.Content.ReadAsStringAsync();
    16.                 Console.WriteLine(data);
    17.             }
    18.         }
    19.     }
    20. }
    21.  
    The full error:
    Code (JSON):
    1.  
    2. {
    3.    "status":400,
    4.    "title":"Bad Request",
    5.    "requestId":"d6d5d2ce-abad-4e86-8fa9-30871129d7ef",
    6.    "detail":"MalformedRequestContentRejection: Cannot deserialize value of type `com.unity.remoteconfig.models.Config` from Array value (token `JsonToken.START_ARRAY`)\n at [Source: (String)\"[[[]],[[]]]\"; line: 1, column: 1]",
    7.    "details":[
    8.      
    9.    ],
    10.    "code":400,
    11.    "type":"https://services.docs.unity.com/docs/errors#400"
    12. }
    13.  
     
  2. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    110
    Hi Noshbar,

    I have a few recommendations for you.

    The first, all the admin APIs and specifications are published here.
    The one you're looking for is this one here.

    My guess is that an empty configuration is not accepted, you could try adding

    Code (JavaScript):
    1. {
    2. "key": "aKey",
    3. "type": "string",
    4. "value": "A String"
    5. }
    OTOH, instead:

    1. I recommend you use the "UGS CLI", which you could use directly, as it is a CLI tool. The "deploy" command allows you to directly deploy a .rc file with the right format. You can create an example file using "ugs rc new-file"
    (be aware that these workflows are also available from the editor using the "Deployment" package and the "Remote Config" package)
    2. Second best option is to generate from specification. The open-api spec can be used to generate a C# client using an OpenAPI generator
    3. The UGS CLI is Open-Source, so here's an example on how we construct the body: link
     
  3. NoshBar

    NoshBar

    Joined:
    Nov 19, 2012
    Posts:
    3
    Hi!

    Those specifications are the ones I mentioned taking the example JSON from, the curl one in the sidelines specifically.
    Alas, as with using the returned existing config, adding anything to the configuration "value" field results in the same error as before.

    1. We're needing to make a standalone tool, so using the editor is out. Before realising it was editor only, we were using the Unity
    RemoteConfigWebApiClient
    to do things, but naturally failed when we tried to make a build using an editor-only class. Using the UGS CLI could be an option, even if it's a new dependency and file format
    2. That's all new to me, thanks for the suggestion!
    3. I'd actually based the JSON generator on this code

    I guess I could look into getting the existing config in one JSON format, then convert it into the .rc format, and call out to the UGS CLI tool, but that feels kinda weird, as we just want to get the existing config, change one field, then push it back, and the "only thing" (probably?) holding us back is the wrong JSON format (I dumped the JToken object to a string, looks fine, online validator says it's fine).

    I know I sound hesitant to try the other solutions, but it's mostly just wondering why this isn't working, and not being able to figure out why.

    I tried using a WebRequest, but then got 404 errors (SSL related I'm guessing?).
    I tried crafting things together using
    ByteArrayContent(Encoding.UTF8.GetBytes(json_string));
    instead (setting application type to JSON), but then got an error stating
    No content to map due to end-of-input\n at [Source: (String)\"\"; line: 1, column: 0]


    So yeah. We'll try using UnityWebRequest (as suggested by someone else), but I'd love to know how to use the REST API from vanilla C#, just because!

    I really appreciate the feedback, thank you!
     
  4. NoshBar

    NoshBar

    Joined:
    Nov 19, 2012
    Posts:
    3
    After all that, success!

    I had to use
    request.Content = new StringContent(json_string, Encoding.UTF8, "application/json");
    instead of the JsonContent.

    Not sure what JsonContent was doing internally, but that did the trick for me.

    Thank you again!
     
    GabKBelmonte likes this.
  5. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    110
    Happy to hear that! Sorry for the late reply, I need to check my alert settings