Hi
I have below code & i want if successfully done then Sucess message should get displayed else if there ie error or Awb no does not exists then it should show some message
C#
var client = new RestClient("api.nimbuspost.com/v1/shipments/cancel");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var body = @"{" + "\n" +
@" ""email"" : ""[email protected]""," + "\n" +
@" ""password"" : ""T!""" + "\n" +
@"awb : " + awb + " \n" +
@"}";
request.AddHeader("Authorization", "Bearer " + token);
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);Thanks

Jomis PjRank #2,956 · 488 reputationPosted Dec 26, 2022, 2:37 AM EST
Please check full code.
var client = new RestClient("api.nimbuspost.com/v1/shipments/cancel"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var body = @"{" + "\n" + @" ""email"" : ""[email protected]""," + "\n" + @" ""password"" : ""T!""" + "\n" + @"awb : " + awb + " \n" + @"}"; request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); var errors = new List(); rm = JsonConvert.DeserializeObject(response.Content.ToString(), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); result.Data = rm.Data; return result;Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 2:57 AM EST
Hi
What is vInstallmentResultModel. What should be the datatype of result.
Thanks
Jomis PjRank #2,956 · 488 reputationPosted Dec 26, 2022, 3:02 AM EST
public class InstallmentResultModel Data { get; set; }
{
public string Message { get; set; }
public int Status { get; set; }
public List
}
Jomis PjRank #2,956 · 488 reputationPosted Dec 26, 2022, 3:05 AM EST
New Code:
var client = new RestClient("api.nimbuspost.com/v1/shipments/cancel"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var body = @"{" + "\n" + @" ""email"" : ""[email protected]""," + "\n" + @" ""password"" : ""T!""" + "\n" + @"awb : " + awb + " \n" + @"}"; request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); var errors = new List(); rm = JsonConvert.DeserializeObject(response.Content.ToString(), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg) { errors.Add(earg.ErrorContext.Member.ToString()); earg.ErrorContext.Handled = true; } }); result.Data = rm.Data; return result;Create Model to return a data in your code.
public class ResultModel { public string Message { get; set; } public int Status { get; set; } }Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 3:17 AM EST
Hi
Where u have defined rm & result.
Thanks
Jomis PjRank #2,956 · 488 reputationPosted Dec 26, 2022, 3:25 AM EST
//Just defined above your code.
JsonResult result = new JsonResult();
ResultModel rm = new ResultModel();
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 3:43 AM EST
Hi Jomis
I am using .net c# not MVC. How this code will work in .net c#
On this line JsonResult result = new JsonResult(); it is saying to install Microsoft.Asp.NetVore.MVC.Formatters.Json.
Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Dec 26, 2022, 4:38 AM EST
Hello
You can use the response to check response is valid or not and as per requirement, you can display the message.
Try the below code to test the response.
Thanks
Amit MohantyRank #112 · 20K reputationPosted Dec 26, 2022, 4:40 AM EST
Hey, if your API returns Status as true or false then try the below code.
private ResultModel YourFunction(string token, string awb) { ResultModel resultModel; var client = new RestClient("api.nimbuspost.com/v1/shipments/cancel"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var body = @"{" + "\n" + @" ""emailid"" : ""[email protected]""," + "\n" + @" ""password"" : ""T!""," + "\n" + @" ""awb"" : " + awb + " \n" + @"}"; request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); var data = response.Content; if (!string.IsNullOrEmpty(data)) { dynamic dynamicData = null; try { dynamicData = JObject.Parse(data); } catch { dynamicData = JObject.Parse("{ \"Status\":false,\"Message\":\"error in response convertion!\"}"); } if (dynamicData.status == true) resultModel = new ResultModel() { Status = true, Message = "Your success message" }; else resultModel = new ResultModel() { Status = false, Message = "your error message" }; } else resultModel = new ResultModel() { Status = false, Message = "empty responce" }; return resultModel; }public class ResultModel { public bool Status { get; set; } public string Message { get; set; } }Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 4:57 AM EST
Hi
It is giving error - cannot implicitly convert type boolean to int on Status = true/False
var data = response.Content; if (!string.IsNullOrEmpty(data)) { dynamic dynamicData = null; try { dynamicData = JObject.Parse(data); } catch { dynamicData = JObject.Parse("{ \"Status\":false,\"Message\":\"error in response convertion!\"}"); } if (dynamicData.status == true) resultModel = new ResultModel() { Status = true, Message = "Your success message" }; else resultModel = new ResultModel() { Status = false, Message = "your error message" }; } else resultModel = new ResultModel() { Status = false, Message = "empty responce" };Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Dec 26, 2022, 5:07 AM EST
Hello
You need to change if condition.
Existing
if (dynamicData.status == true)Change it with
if (dynamicData.status == 200)Conclusion, response.status is number type and it will return HTTP response number code.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 5:10 AM EST
Hi Vishal
It is still giving error on Status = true, / Status = false,
if (dynamicData.status == 200) resultModel = new ResultModel() { Status = true, Message = "Your success message" }; else resultModel = new ResultModel() { Status = false, Message = "your error message" };Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Dec 26, 2022, 5:27 AM EST
Hello Romco
Can you please check response.Status. please check the value if it's number then set if condition like below
if (response.status == 200)Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 5:50 AM EST
Hi Vishal
It is showing response.status = OK
response.description ="Ok"
Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Dec 26, 2022, 6:10 AM EST
The HTTP 200 OK success status response code indicates that the request has succeeded.
So it's the same as 200. you can check if (response.status == "OK")
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 26, 2022, 6:17 AM EST
Hi Vishal
I have checked now in var data = response.Content; it is showing data = ""
Thanks