Hi, I've been using the code that @Warren provided (which has been really helpful so far!) but I'm a bit stuck and I was hoping that someone can help and show me where I'm going wrong!!
My full code is below. Few things I've had to add/alter.
I replaced GET with PUT in my request:
string resultsReturned = RunQuery("rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=5&startAt=0", "", "", "PUT");Because when I leave GET in place I get this error:
{"Cannot send a content-body with this verb-type."}
I also had to put this in as I couldn't get a tls/ssl connection without it:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
However I'm still not returning any data. I'm very stuck! Can someone help me?
Full code that I have:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JIRA_service_desk
{
public partial class WebForm9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string resultsReturned = RunQuery("rest/api/2/search?jql=project=RES&fields=key,summary&maxResults=5&startAt=0", "", "", "PUT");
resultsTest.InnerHtml = resultsReturned;
}
public static string RunQuery(string query, string argument = null, string data = null, string method = "GET")
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string m_BaseUrl = "https://myurl.ac.uk/";
try
{
m_BaseUrl = m_BaseUrl + query;
HttpWebRequest newRequest = WebRequest.Create(m_BaseUrl) as HttpWebRequest;
newRequest.ContentType = "application/json";
newRequest.Method = method;
if (data != null)
{
using (StreamWriter writer = new StreamWriter(newRequest.GetRequestStream()))
{
writer.Write(data);
}
}
string base64Credentials = GetEncodedCredentials();
newRequest.Headers.Add("Authorization", "Basic " + base64Credentials);
HttpWebResponse response = newRequest.GetResponse() as HttpWebResponse;
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
newRequest = null;
response = null;
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
private static string GetEncodedCredentials()
{
string m_Username = "hollie@email.co.uk";
string m_Password = "";
string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
}
}