Uploading an attachment in C# URGENT

Hey! I have been trying to upload an attachment( text or png) to my confluence page, but it is giving me an error. I am trying to send the request in the following format. Please let me know if it requires some changes?
 This is the code snippet in C# : 

string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Credentials = CredentialCache.DefaultCredentials;
req.Headers.Add("X-Atlassian-Token", "nocheck");
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
string contentDisp = "Content-Disposition: form-data; name=\"file\"; filename=\"foo.txt\"";
string contentType = "Content-Type: text/plain";
string fullContents = "{0}\r\n{1}\r\n{2}\r\n\r\n{3}\r\n{4}--", boundary, contentDisp,contentType, Encoding.Default.GetString(contents), boundary;
req.ContentLength = fullContents.Length;
using (StreamWriter reqStream = new StreamWriter(req.GetRequestStream()))

var httpResponse = (HttpWebResponse)req.GetResponse(); //403 Error

1 answer

1 vote

You haven't told us what error you're getting.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Friday in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

274 views 11 10
Join discussion

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