0

I have been searching and working on how to get Jira REST response for quite some time now. The problem is when it gets to this code, an exception will be raised, either BAD REQUEST or INTERNAL SERVER ERROR.

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

It never go beyond this code. Instead, I am expecting:

}
  "errorMessages": [],
  "errors": {
  "message": "An error occured ... "
  }
}

for error messages or:

}
  "id": "11600",
  "key": "RP-547",
  "self": "http://jira.com/rest/api/2/issue/11600"
}

on success.

Is there some thing I missed, or misunderstood? How do I get the expected results?

Some extra info:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Accept = "application/json";
request.Method = method; //POST
if (data != null)
{
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(data);
    }
}
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", "Basic " + base64Credentials);
string result = string.Empty;
HttpWebResponse response = request.GetResponse() as HttpWebResponse; //breaks here
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    result = reader.ReadToEnd();
}
Innovit
  • 93
  • 2
  • 13
  • When you say "It never go beyond this code" I thought you meant that your program hangs, which is the issue that I experienced. You should close the writer. See https://stackoverflow.com/questions/3213812/c-sharp-httpwebrequest-getresponse-freezes-and-hangs-my-program. Even if this information is not entirely relevant to the original question, I have placed the information here because this question relates to the usage of the JIRA REST API. Unfortunately, this example of processing a httpresponse appears to be spread about everywhere on the net. – B5A7 Jan 22 '19 at 22:17

1 Answers1

0

HttpWebRequest generates an exception for non 200 http responses. You need to process error responses in a catch clause.

From the help page:

Note If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

See also this question

Alternately you can use HttpClient instead of WebRequest if you're using .Net 4.5.

Community
  • 1
  • 1
Eli Algranti
  • 8,707
  • 2
  • 42
  • 50