C
C#2y ago
Scythe

JSON child objects within a RestRequest body

I have a working API call in Postman that gets data from another application using a filter. Using the C# code that Postman outputs works, but doesn't seem to include the filter object within my json body. So with the example below: C# code
var client = new RestClient("http://localhost:XXXX/v1/search");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
var body = @"{""source"": ""non-archived"", ""fields"": [""type"", ""title"", ""tags"", ""cuepoints"", ""data""], ""filter"": { ""artist"": ""Dr. Peacock"", ""title"": ""Medication"" } }";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var client = new RestClient("http://localhost:XXXX/v1/search");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
var body = @"{""source"": ""non-archived"", ""fields"": [""type"", ""title"", ""tags"", ""cuepoints"", ""data""], ""filter"": { ""artist"": ""Dr. Peacock"", ""title"": ""Medication"" } }";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
JSON Body in Postman
{
"source": "non-archived",
"fields": ["type", "title", "tags", "cuepoints", "data"],
"filter": { "artist": "Dr. Peacock", "title": "Medication" }
}
{
"source": "non-archived",
"fields": ["type", "title", "tags", "cuepoints", "data"],
"filter": { "artist": "Dr. Peacock", "title": "Medication" }
}
When run in Postman, I correctly get 3 results returned that match the filter arguments. When run in C# I get 18,000 results as the filter object doesn't appear to be passed correctly and it's only getting the source/field values. I'm assuming I need to do something else to wrap the filter object within the json body object so that it's parsed correctly but I haven't been able to work out what that syntax should be
3 Replies
Timtier
Timtier2y ago
It looks like your filter object should just be a simple class or record containing (optional) strings; artist and title If you want to make it generic and let whatever consumes the filter do mapping, a dictionary of string keys and string values would also work
Scythe
Scythe2y ago
The API that consumes the filter isn't in C# and just needs the JSON body as I have it in Postman, but for whatever reason the way I'm adding it to the RestRequest in C# is not producing the same result. Are you able to show me an example of what that dictionary method would look like?
Timtier
Timtier2y ago
I think just making something along the lines of this might be easiest: { "source": "non-archived", "fields": ["type", "title", "tags", "cuepoints", "data"], "filter": { "artist": "Dr. Peacock", "title": "Medication" } }
Want results from more Discord servers?
Add your server
More Posts