0

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);
    }
}

Edit 3

I tried the following here but it just came back null when i no it should be filled with the bleow errors from the json in my first post.

  catch (WebException ex){

            var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
            ErrorMessage error = JsonConvert.DeserializeObject<ErrorMessage>(resp);
            var fieldinformation = error.PropertyName.ToString();
            var messageFromServer = error.Reason.ToString();

        }

1 Answers1

0

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
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I was just tesing that approach but that does not get me the information within errors it is this i am requiring to send back to my client –  Oct 22 '16 at 12:55
  • please see my above edit to see how i can delserilize to my list object please. –  Oct 22 '16 at 12:58