update worklog C# "The remote server returned an error: (401) Unauthorized."

Im going to update worklog of an issue;

HttpWebResponse return this error "The remote server returned an error: (401) Unauthorized.".
Can anyone help me please?

Here is my code:

protected string RunQuery(JiraResource resource, string issue_id, string data = null, string method = "PUT")
{
//resource = issue
  // data :{"worklog":{"add":{"comment":"Sample test comment by IJ","started":"2014-01-28T19:25:33.78125+05:30","timeSpent":210}}}
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());
if (issue_id != null)
            {
                url = string.Format("{0}{1}", url, issue_id);
            }
   // url : https://company.atlassian.net/rest/api/2/issue/JIRA-16
            
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ContentType = "application/json";
            request.Method = method;
            request.ContentLength = data.Length;
 
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(data);
            }
 
            string base64Credentials = GetEncodedCredentials();
            request.Headers.Add("Authorization", "Basic " + base64Credentials);
 
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 
            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
 
            return result;
 
        }

where;

private string GetEncodedCredentials()
        {
            string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
            byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
            return Convert.ToBase64String(byteCredentials);
        }

2 answers

1 accepted

0 votes
Accepted answer

I should have add authorization header before sending request stream.
like below;

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.ContentType = "application/json";
        request.Method = method;
        request.ContentLength = data.Length;

        string base64Credentials = GetEncodedCredentials();
        request.Headers.Add("Authorization", "Basic " + base64Credentials);

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write(data);
        }
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Jira

Make your Atlassian Cloud products more secure: our NEW admin security guide

Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...

45 views 0 2
Read article

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