Hi
Below response is returned by API . I want to display Error message . I am using Asp.net Web Based Applcation C#.
C#
{"status":false,"message":"Pickup Address is required\nPickup State is required\nThe Pickup Pincode field must be exactly 6 characters in length.\n"}If status is O.K then successful message should be displayed
Thanks

Amit MohantyRank #112 · 20K reputationPosted Jan 10, 2023, 2:21 AM EST
Check this
var client = new RestClient("your url"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("content-type", "application/json"); var response = client.Execute(request); if (response.IsSuccessful) { var dDynamic = JsonConvert.DeserializeObject(response.Content); if(dDynamic["status"]) Console.WriteLine(string.Format("Success : {0}", dDynamic["message"].ToString())); else Console.WriteLine(string.Format("Failure : {0}", dDynamic["message"].ToString())); } else { // to do code after get error Console.WriteLine(string.Format("Failed. Code : {0}", response.StatusCode)); }Pasang TamangRank #449 · 5.6K reputationPosted Jan 10, 2023, 2:19 AM EST
Hi,
Using Newtonsoft.Json, you should be able to achieve what you want. Please check below code for example
public class ResponseModel { [JsonPropertyName("status")] public bool Status { get; set; } [JsonPropertyName("message")] public string Message { get; set; } }var response = "{\"status\":false,\"message\":\"Pickup Address is required\\nPickup State is required\\nThe Pickup Pincode field must be exactly 6 characters in length.\\n\"}"; var responseContent = JsonConvert.DeserializeObject(response); if (responseContent.Status == false) { var message = responseContent.Message; }Regards,
Pasang
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 10, 2023, 6:05 AM EST
Hi Amit
I am getting error IsSucessful response doed not exist in the current context
Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Jan 10, 2023, 7:28 AM EST
Hello
You can try the below code for GET requests.
static async Task GetProductAsync(string path) { Product product = null; HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { product = await response.Content.ReadAsAsync(); } return product; }Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 12, 2023, 1:36 AM EST
Please refer below code.
Create a class to parse your Response.
public class Response { [JsonPropertyName("status")] public bool Status { get; set; } [JsonPropertyName("message")] public string Message { get; set; } }Code to Get data from URL and parse it to valid data formate.
string requestURL = "Your GET API URL"; //EX: https://test.api.com/api/users string urlParameters = "Your Query string parameters"; //EX: ?user_id=10 Or string.Empty using (var client = new HttpClient()) { client.BaseAddress = new Uri(requestURL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); // Get response from requested URL var response = client.GetAsync(urlParameters).Result; //If request is success if (response.IsSuccessStatusCode) { //var sampleResponse = response.Content.ReadAsStringAsync().Result; var sampleResponse = "{\"status\":false,\"message\":\"Pickup Address is required\\nPickup State is required\\nThe Pickup Pincode field must be exactly 6 characters in length.\\n\"}"; var responseContent = JsonConvert.DeserializeObject(sampleResponse); if (responseContent.Status) { Console.WriteLine("Successful responses received"); } else { Console.WriteLine($"Error from responses {responseContent.Message}"); } } }Please let me know in case of any issue. Thanks :)