0

I'm attempting to update my RestSharp calls to v107 and noticed a slight difference in how parameters are added. What I have working in 106 doesn't appear to work in 107.

What I have is that I'm adding an object with nested objects to a POST request. My object looks like this:

class CallParameters
{
public CallResources Resources {get;set;}
public CallFilters Filters {get;set;}
}

class CallResources 
{
public bool IncludeOrgData {get;set;}
public bool IncludeDemoData {get;set}
}

class CallFilters
{
public string LastModified {get;set}
public List<string> Users {get;set;}
} 

I know I can serialize that (once I set the fields) to look like:

{"Resources" : {"IncludeOrgData" : true, "IncludeDemoData" : true}, "Filters" : { "LastModifed" : "1/1/21", "Users" : ["User1", User2", "User3"]}}

With 106, I was able to create my rest call by serializing my object class to a string and adding it via AddJsonBody like this

CallParameters NewCallParameters = new CallParameters();
...Set the fields within the class...
string JsonBody = JsonConvert.SerializeObject(NewCallParameters);
var request = new RestRequest { Method = Method.POST };
var client = new RestClient();
var response = client.Execute(request.AddJsonBody(JsonBody));

Worked great. Passed over all the parameters right

In 107 I know it's slightly different. Using the same serialize and AddJsonObject, I show a single parameter is added with no name, and the value of the whole serialized object. This causes my call to return

The request is invalid - An error has occurred

If I use 'AddObject' to add the entire class CallParamters class as a whole, when I trace through things and look at my request parameters collection, I do see two parameters listed. They are named correctly ('Resources' and 'Filters') but the values are 'MyApp.CallResources' and 'MyApp.CallFilters'. It's like they are not serializing when adding. This returns:

At least one additional resource is required: IncludeOrgData, IncludeDemoData

If I add the objects separately as 'CallResourses' and 'CallFilters', I wind up with four parameters names for all the fields within. Value is correct aside from the Users List, which just shows 'System.Collections.Generic.List[System.String]'. Also having four parameters doesn't seem right either and the call also fails with invalid parameters. This returns

The request is invalid - Resources field is required

I have experimented with 'AddParamter' some. If I add the whole class as a parameter I'm not sure what to name it. Also the call fails since it has no name. If I add them separately like this:

string MyResources = JsonSerializer.Serialize(NewCallParameters.Resources);
string MyFilters = JsonSerializer.Serialize(NewCallParameters.Filters);
request.AddParamter("Resources", MyResources );
request.AddParamter("Filters", MyFilters);

I do get two parameters named correctly. The values look good and show all the entries. The return result though from the Rest service I'm calling states

At least one additional resource is required: IncludeOrgData, IncludeDemoData'

So it seems it's not seeing the values in the 'Resources' parameter. Do I need to add each as a separate parameter? How would I add nested parameters that way? I feel like I'm close but not getting something.

Seril
  • 499
  • 4
  • 9
  • 22

1 Answers1

0

It helps to read the documentation. RestSharp has embedded serialization. Don't serialize your object when using AddJsonBody. As you need Pascal-case fields, you'd need to either change JSON options for the default serializer or use the RestSharp.Serializers.NewtonsoftJson package.

If you prefer to add a pre-serialized string, use AddStringBody

enter image description here

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83