JIRA Rest API. Retrieve errormessages returned by API when it fails, using c# or vb.net

453 views Asked by At

I'm developing a small app that pulls issues from JIRA from a given list of issue ids, however some of them don't exist and the API call returns a 400 Bad Request message.

This is the relevant block of code I have so far (C#):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(thejiraurl);
request.Method = "GET";
request.ContentType = "Content-Type: application/json";
request.Accept = "application/json";
string mergedCredentials = "thecredentials :)";
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
string encodedCredentials = Convert.ToBase64String(byteCredentials);
request.Headers.Add("Authorization", "Basic " + encodedCredentials);

try
{
  WebResponse response = request.GetResponse(); //this line breaks because some records don't exist anymore, that's ok, but at least I need to know which ones! those are found in the response that I can't see here but I can with Fiddler! @.@
  Stream data = response.GetResponseStream();
}
catch(WebException ex)
{
  Console.WriteLine(ex.Message);
  if(ex.Status == WebExceptionStatus.ProtocolError)
  {
    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)ex.Response).StatusCode); //This is 'BadRequest'
    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)ex.Response).StatusDescription); //This is 'Bad Request'
    using (Stream responseStream = ex.Response.GetResponseStream())
    using (StreamReader responseReader = new StreamReader(responseStream))
    {
      string responseMessage = responseReader.ReadToEnd(); //This is a long message but do not has the errormessages seen with Fiddler (and even in a internet browser)
      Console.WriteLine(responseMessage);
    }
  }
}

However when I use Fiddler I can see the actual errormessages returned, that looks like:

{"errorMessages":["A value with ID '1' does not exist for the field 'key'."],"errors":{}}

How can I get this messages using C# or vb?

This is very similar to question, but that one is with Java: https://answers.atlassian.com/questions/147793/retrieving-errormessages-when-a-call-to-the-rest-api-fails-using-httpurlconnection

I appreciate any help.

0

There are 0 answers