RestSharp v107 添加嵌套 JSON/对象的参数问题
我正在尝试将RESTSHARP调用更新为V107,并注意到添加参数的略有不同。我在106中工作的工作似乎在107中无法使用。
我的所在是,我正在将带有嵌套对象的对象添加到邮政请求中。我的对象看起来像这样:
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;}
}
我知道我可以序列化(一旦设置了字段),就可以看起来像:
{"Resources" : {"IncludeOrgData" : true, "IncludeDemoData" : true}, "Filters" : { "LastModifed" : "1/1/21", "Users" : ["User1", User2", "User3"]}}
使用106,我能够通过将我的对象类序列化为字符串来创建我的休息调用,并通过addjsonbody添加它
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));
,伟大的。 在107中,
我知道它略有不同。使用相同的序列化和AddJsonObject,我显示了一个没有名称的单个参数,以及整个序列化对象的值。这会导致我的呼叫返回
请求无效 -
如果我使用'addobject'来添加整个类CallParamters类,当我跟踪事物并查看我的请求参数时, 发生了错误收集,我确实看到了列出的两个参数。它们的命名正确(“资源”和“过滤器”),但值是“ myapp.callresources”和“ myapp.callfilters”。就像他们在添加时不会序列化。此返回:
至少需要另外一个资源:包括orgdata,inclymodata ,
如果我将对象分别添加为“ callresourses”和“ callfilters”,我将在其中所有字段中添加四个参数名称。除了用户列表之外,值是正确的,该列表仅显示'system.collections.generic.list [system.string]'。同样,有四个参数似乎也不正确,并且无效参数也会失败。此返回
请求是无效的 - 需要资源字段
我已经尝试了一些“ AddParamter”。如果我将整个课程添加为参数,则不确定该名字命名。此外,呼叫失败,因为它没有名称。如果我这样单独添加它们:
string MyResources = JsonSerializer.Serialize(NewCallParameters.Resources);
string MyFilters = JsonSerializer.Serialize(NewCallParameters.Filters);
request.AddParamter("Resources", MyResources );
request.AddParamter("Filters", MyFilters);
我确实会正确命名两个参数。值看起来不错,并显示所有条目。返回结果虽然我称为状态
至少需要一个附加资源的返回结果:包括orgdata,InvernaldeModata',
因此似乎看不到'Resources'参数中的值。我需要添加每个参数吗?如何以这种方式添加嵌套参数?我觉得我很亲密,但没有得到一些东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它有助于读取文档。 RestSharp已嵌入序列化。使用
addjsonbody
时,请勿序列化对象。由于您需要Pascal-Case字段,因此需要更改默认序列化器的JSON选项,或使用RestSharp.Serializers.newtonsoftjson
package。如果您希望添加预序列化的字符串,请使用
addstringbody
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 theRestSharp.Serializers.NewtonsoftJson
package.If you prefer to add a pre-serialized string, use
AddStringBody