trycatch39ni trycatch39ni - 1 month ago 16
C# Question

How do I retrieve json based errors instead of standard .net

I am hitting a web services that returns its errors as a json. How can I interpret these errors when I am using a web client? I want to be able to pass these onto my users so they no the errors they are getting.


{ "success": false, "errors": [

{

"propertyName": "Reference",

"reason": "'Reference' must not be empty."

},

{

"propertyName": "Reference",

"reason": "'Reference' should not be empty."

},
{
"propertyName": "Key",

"reason": "'Key' must be between 10 and 15 characters. You entered 3 characters."

},
{
"propertyName": "Key",

"reason": "Key does not start with zero: 123."
} ], "warnings": [], "information": []


This is how I am calling out to the third party api but when I hit this line I'm getting a standard .net error throw back my question is how can I instead get the values of the above json back

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

Screen shot as i cannot upload

https://snag.gy/Itoy02.jpg

public void TransferToSlate(string json, string url)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}


Edit 2
I have tried the following and its got me a bit futher but not to the desps of the two list propertyes within errors

public void TransferToSlate(string json, string url)
{

try {
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}


var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (WebException ex){

var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;

}


So I created this class but im not to sure how to deserilize errors to it

public class ErrorMessage
{
public string PropertyName { get; set; }

public string Reason { get; set; }

public override string ToString()
{
return string.IsNullOrWhiteSpace(PropertyName)
? Reason
: string.Format("{0} : {1}", PropertyName, Reason);
}
}

Answer

You can read the response from the WebException:

try
{
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}
catch(WebException ex)
{
    var httpResponse = ex.Response as HttpWebResponse;
    if (httpResponse != null)
    {
        // process the response
    }
}
Comments