Search code examples
c#jsonrest-clientuipath

C# An unexpected 'StartObject' node was found for property 'InputArguments' when reading from the JSON reader. A 'PrimitiveValue' node was expected


I am using RestClient to pass JSON paramter into api in C#.But I am getting response

"An unexpected 'StartObject' node was found for property named 'InputArguments' when reading from the JSON reader. A 'PrimitiveValue' node was expected"

I am using below code in C#

var client_startRobot = new RestClient("https://xxxx.xxxx.com/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs");
var request_startRobot = new RestRequest(Method.POST) ;
request_startRobot.AddParameter("Authorization", string.Format("Bearer " + result), ParameterType.HttpHeader);
request_startRobot.AddHeader("content-type", "application/json");
string parameter = "{\"startInfo\":{\"ReleaseKey\": \"ds32rd1-6c98-42f542d-23bb8111ac91d\",\"RobotIds\": [1],\"JobsCount\": 0,\"Strategy\": \"Specific\",\"InputArguments\": {\"add_name\": \"xxxxx-xxx-\"}}}";
request_startRobot.AddParameter("application/json; charset=utf-8", parameter, ParameterType.RequestBody);
IRestResponse response_startRobot = client_startRobot.Execute(request_startRobot);

Solution

  • This seems to be a question of reading the API documentation carefully. Assuming you are trying to call an orchestrator as described here, I find this example that looks a lot like yours.

    { "startInfo":
       { "ReleaseKey": "5b754c63-5d1a-4c37-bb9b-74b69e4934bf",
         "Strategy": "Specific",
         "RobotIds": [ 1553 ],
         "NoOfRobots": 0,
         "Source": "Manual",
         "InputArguments": "{\"message\":\"Aloha\"}"
       } 
    }
    

    Note that the InputArguments value is actually a simple string, not actual JSON (the string contains an escaped JSON string).

    Your request looks like this:

    "InputArguments": {"add_name": "xxxxx-xxx-"}
    

    When according to the example given, it should look like this:

    "InputArguments": "{\"add_name\": \"xxxxx-xxx-\"}"
    

    It looks like you will have to "double escape" this part of your string, something like this:

    \"InputArguments\": \"{\\\"add_name\\\": \\\"xxxxx-xxx-\\\"}\"
    

    Actually building up a strongly typed request object and leaving the serialization to your REST client might make things easier to read.