Struggling to update RemoteConfig via REST API

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:

var jt = JToken.Parse("{\r\n  \t\"type\": \"settings\",\r\n  \t\"value\": []\r\n  }");
var request = new HttpRequestMessage(HttpMethod.Put, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", fullKey64);
request.Content = JsonContent.Create(jt);

try
{
    using (HttpClient client = new HttpClient())
    {   
        using (HttpResponseMessage response = await client.SendAsync(request))
        {
            using (HttpContent content = response.Content)
            {
                var data = await response.Content.ReadAsStringAsync();
                Console.WriteLine(data);
            }
        }
    }
}

The full error:

{
   "status":400,
   "title":"Bad Request",
   "requestId":"d6d5d2ce-abad-4e86-8fa9-30871129d7ef",
   "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]",
   "details":[
     
   ],
   "code":400,
   "type":"https://services.docs.unity.com/docs/errors#400"
}

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

{
"key": "aKey",
"type": "string",
"value": "A String"
}

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

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!

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!

1 Like

Happy to hear that! Sorry for the late reply, I need to check my alert settings