Sasi.N
(SK)
August 27, 2024, 9:13pm
1
Hi,
I am trying to call a rest API (outside) from a function. The POST call has a json payload which is nested. I am having hard time getting it to work. I have tried different ways and get the same error (Event viewer) as below.
Code:
url = $"https://actualurl.com/method";
client = new RestClient (url);
client.Timeout = -1;
request = new RestRequest (Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("content-type", "application/json");
request.AddHeader("Authorization", access_token);
/*
string jsonBody = @"
{
""includeDetail"": true,
""Info"": [
{
""Number"": {
""forNumber"": ""123456""
}
}
]
}
";
*/
var trackingNumberInfo = new { trackingNumber = "733868333834"};
var trackingInfo = new { trackingInfo = trackingNumberInfo };
var bodyArray = new { includeDetailedScans = true, trackingInfo };
var jsonBody = JsonConvert.SerializeObject(bodyArray);
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
var error = "";
try
{
response = client.Execute (request);
}
catch (Exception ex)
{
error = "Exception caught 2: {0}" + ex;
}
finally
{
jsonValue = JsonConvert.DeserializeObject(response.Content);
this.output = jsonValue.transactionId;
}
Error:
Exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for ‘RestSharp.RestRequest.AddHeader(string, string)’ has some invalid arguments
at CallSite.Target(Closure , CallSite , RestRequest , String , Object )*
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)*
Any advice/guidance is much appreciated.
Thanks, SK.
klincecum
(Kevin Lincecum)
August 27, 2024, 10:09pm
2
string access_token = "whatever";
string url = $"http://actualurl.com/method";
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("content-type", "application/json");
request.AddHeader("Authorization", access_token);
var trackingNumberInfo = new { trackingNumber = "733868333834"};
var trackingInfo = new { trackingInfo = trackingNumberInfo };
var bodyArray = new { includeDetailedScans = true, trackingInfo };
var jsonBody = JsonConvert.SerializeObject(bodyArray);
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
var error = "";
try
{
var response = client.Execute (request);
dynamic jsonValue = JsonConvert.DeserializeObject(response.Content);
this.output = jsonValue.transactionId;
}
catch (Exception ex)
{
error = "Exception caught 2: {0}" + ex;
}
2 Likes
Sasi.N
(SK)
August 28, 2024, 2:19am
3
Thank you @klincecum . That worked like a charm.
Now, I have the response as a nested json value. I am null value error if I traverse.
jsonResponse = JsonConvert.DeserializeObject(response.Content);
var City = jsonResponse.output[0].city;
I am researching it.
Thanks again for your quick help!!
SK.
1 Like
klincecum
(Kevin Lincecum)
August 28, 2024, 2:20am
4
Post the json and what you want out of it.
We can knock that out too.
Sasi.N
(SK)
August 28, 2024, 2:28am
5
Here it is:
{
"transactionId": "A3c00f",
"output": {
"alerts": [
{
"code": "RESPONSE",
"message": "This is a Response."
}
],
"completeTrackResults": [
{
"trackingNumber": "73860713834",
"trackResults": [
{
"trackingNumberInfo": {
"trackingNumber": "73860713834",
"trackingNumberUniqueId": "12834~ADEG"
},
"lastUpdatedDestinationAddress": {
"city": "GREENWOOD",
"stateOrProvinceCode": "IN",
"countryName": "United States"
},
"goodsClassificationCode": "",
"returnDetail": {}
}
]
}
]
}
}
Error: Exception caught 2: {0}Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference\r\n at CallSite.Target(Closure , CallSite , Object )\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
Interested in getting lastUpdatedDestinationAddress.city.
Thanks, SK.
klincecum
(Kevin Lincecum)
August 28, 2024, 2:50am
6
//Add using Newtonsoft.Json.Linq; to your usings section
var response = client.Execute(request);
var jsonAsTokens = JToken.Parse(response.Content);
this.output = jsonAsTokens["transactionId"].Value<string>();
string city = jsonAsTokens["output"]["completeTrackResults"][0]["trackResults"][0]["lastUpdatedDestinationAddress"]["city"].Value<string>();
3 Likes
Sasi.N
(SK)
August 28, 2024, 5:21pm
7
Thank you bunch, @klincecum . Works perfectly!!
-SK
1 Like