title: Handle json error message from REST API through HTTPWebRepsonse
body: <p>I am consuming REST API (provided by client) in C#/asp.net and manipulate json result returned by that REST API. i have consume it by following code.</p>
  <pre><code>     HttpWebResponse res = null;
       string ReturnBody = string.Empty;
  
     string requestBody = string.Empty;
                  WebRequest request = WebRequest.Create(Path);
                  request.ContentType = &quot;application/json&quot;;
                  request.Method = &quot;POST&quot;;
                  request.ContentLength = json.Length;
  
                  //Add Basic Auhtentication header
                  string authInfo = Username + &quot;:&quot; + Password;
                  authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                  request.Headers[&quot;Authorization&quot;] = &quot;Basic &quot; + authInfo;
  
                  System.IO.StreamWriter sw = new System.IO.StreamWriter(request.GetRequestStream());
                  sw.Write(json);
                  sw.Close();
                  res = (HttpWebResponse)request.GetResponse();
                  if (res != null)
                  {
                      using (StreamReader sr = new StreamReader(res.GetResponseStream(), true))
                      {
                          ReturnBody = sr.ReadToEnd();
                          StringBuilder s = new StringBuilder();
                          s.Append(ReturnBody);
                          sr.Close();
                      }
                  }
  </code></pre>
  <p>I have put above code in try catch block, so it works properly if it will return success code(200) so i can consume json response from <strong>res</strong> object as per above code</p>
  <p>but when that REST API gives error then it will redirect to catch and <strong>res</strong> will be <strong>null</strong> so i can not access json response of error message as i can get it by <strong>Fiddler</strong> as per shown in below fig.</p>
  <p><img src="https://i.stack.imgur.com/3dI0B.png" alt="REST test through fiddler" /></p>
  <p>so help me about <strong>How can i consume that json error response through my code?</strong></p>
  <p>Thanks in Advance! for any help.</p>