JIRA REST C# Issue Creation

I have trouble using C# to create an issue. The issue type has "Wiki Renderers" that I am trying to set.

I have tried to use the C# HttpWebRequest method "POST" and it never worked. Here's a code snippet:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "POST";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(jsonData.ToString());
request.ContentLength = byte1.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byte1, 0, byte1.Length);
dataStream.Close();

string mergedCredentials = string.Format("{0}:{1}"
	, _atpSection.AtpTest.AtpList.JiraUser
	, _atpSection.AtpTest.AtpList.JiraPassword);
byte[] byteCredentials = Encoding.UTF8.GetBytes(mergedCredentials);
string base64Credentials = Convert.ToBase64String(byteCredentials);
request.Headers.Add("Authorization", "Basic " + base64Credentials);

HttpWebResponse response = null;
try
{
	response = request.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
	Debug.Print(e.ToString());
}
string StatusDescription = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();

Debug.Print(responseFromServer);

Since this didn't work I used curl.exe instead. Once I get it to work right here I'll try to get the C# to work. Here is the curl.exe snippet:

string tmpFile = Path.GetTempFileName(); // @"C:\tmp.json";
StreamWriter sw = new StreamWriter(tmpFile);
sw.Write(jsonData.ToString());
sw.Close();

sb.Clear();
sb.AppendFormat("-D- -X POST -u {0}:{1} --data @\"{2}\" -H \"Content-Type: application/json\" {3}"
	, _atpSection.AtpTest.AtpList.JiraUser
	, _atpSection.AtpTest.AtpList.JiraPassword
	, tmpFile, url);
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.FileName = _atpSection.AtpTest.AtpList.JiraCurlPath; // @"C:\bin\curl\curl.exe";
psi.Arguments = sb.ToString();
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = psi;
_curlStdOut.Clear();
p.OutputDataReceived += CurlOutputDataReceived;
p.Start();
p.BeginOutputReadLine();
string stderr = p.StandardError.ReadToEnd();
p.WaitForExit();
File.Delete(tmpFile);

When I use curl the issue is created but all the CR-LF are removed. When I create a WIKI table it gets messed up.

How can I preserve the CR-LF when using curl.exe?

Can you see anything obviously wrong with the C# snippet?

3 answers

1 accepted

0 votes
Accepted answer

It'll be really important to use the --data-binary flag; otherwise, the line breaks are thrown out. Here's an example of a corrected cURL command:

curl -D- -u user:pass -X POST -H "Content-Type: application/json" --data-binary @ngr-ver-XX.test http://jiraserver:8080/rest/api/latest/issue

This page doesn't work well in Chrome and the HTML tags are in the source. I can't figure out how to fix it but I think it is due the the web server

Escape with string "\r\n"

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in United States

Administrators Cleanup

Good morning, After a long weekend cleaning up, I am still in the cleanup mode 😊. I am curious to know: When do you feel the urgency to cleanup your Atlassian Tools? How many times do y...

37 views 1 1
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you