Reading images (attachments) via API not working using C#

Hi

I am able to successfully use the Confluence API to read the contents of a page. For a page that has an embedded image, I understand that it is saved in Confluence as an attachment and I’m able to get the URL of the attachment.

My code below, assuming json.results contains all the attachments for a page

                foreach (var attachment in json.results)
                {
                    string imageUrl = "https://coname.atlassian.net/wiki" + attachment._links.download;
                    string saveLocation = @"D:\" + attachment.title;

                    byte[] imageBytes;
                    HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
                    WebResponse imageResponse = imageRequest.GetResponse();
                    Stream responseStream = imageResponse.GetResponseStream();
                    using (BinaryReader br = new BinaryReader(responseStream))
                    {
                        imageBytes = br.ReadBytes(500000);
                        br.Close();
                    }
                    responseStream.Close();
                    imageResponse.Close();

                    File.WriteAllBytes(saveLocation, imageBytes);
                }

The method to read the results to a byte array I found via a Google search, I write a jpg or png file, but trying to open it gives the error below

Screenshot_8

Could someone suggest how to resolve this or a better way of doing it?

Thanks

As an update, I think the issue is that I’m not authenticating to Confluence, so it’s returning something, but not the attachment.

How do I pass in authentication information?

@warren.levy, How are you authenticating for the normal REST API calls?

Hi @ibuchanan

I am using a stand-alone C# desktop application, so whilst running Jira and Confluence API calls, I use Basic authentication with 64 bit encoded email address / API token - this has worked successfully for ages.

I’m unsure how to authenticate when getting the attachments because it isn’t an API call.

Any help greatly appreciated.

I’ve finally managed to sort this

@warren.levy, please share!

The code I showed above is not the same as what I use to read the contents of a Confluence page, but I discovered that using the same code works if I read the output directly into a byte array, as opposed to a string, so the code looks like this

HttpWebRequest newRequest = WebRequest.Create(m_BaseUrl) as HttpWebRequest;
newRequest.ContentType = "application/json";
newRequest.Method = "GET";
newRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

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

HttpWebResponse response = newRequest.GetResponse() as HttpWebResponse;

byte[] image;
Stream responseStream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream))
{
       image = br.ReadBytes(500000);
       br.Close();
}

The byte array image then has valid image data which can be written to a file

1 Like

I’m glad you got it working with API tokens and basic auth. If you do need to switch to OAuth, you now (like right now) have this option: