Sharepoint Online --> Confluence Cloud Sync using C# Rest API (Not working)

Aman Varama August 10, 2022

Hello, 

Trying to sync (Create and Update existing docs, excels, media etc) Sharepoint online documents with Confluence online using REST API. Below is my code. The code can authenticate successfully with confluence but not creating/uploading attachment. 

private static async Task<string> GetContent(string Username, string APIKey)
{

string filePath = @"C:\TestDocument.docx";
var filebytes = File.ReadAllBytes(filePath);
string UserName = Username;
string Password = APIKey;

HttpClient httpClient = new HttpClient();
//Create uri
var builder = new UriBuilder("https://test.atlassian.net/wiki/rest/api/content/264966/child/attachment");
var query = HttpUtility.ParseQueryString(builder.Query);
var fileName = Path.GetFileName(filePath);
query["file-name"] = fileName;
builder.Query = query.ToString();
string url = builder.ToString();

//Do post
try
{
using (var content = new ByteArrayContent(filebytes))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", UserName, Password))));
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Headers.Add("X-Atlassian-Token", "nocheck");
HttpResponseMessage responseString = await httpClient.PostAsync(url, content);
var contents = await responseString.Content.ReadAsStringAsync();
JObject newDocument = JObject.Parse(contents);
return (string)newDocument;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new OkObjectResult(ex.Message);
}


Here is the error/HttpResponseMessage message .





{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Wed, 10 Aug 2022 08:56:02 GMT
Server: globaledge-envoy
Atl-Confluence-Via: h:confluence-prod-ap-1-1.prod.atl-paas.net
X-Envoy-Upstream-Service-Time: 135
Expect-Ct: report-uri="https://web-security-reports.services.atlassian.com/expect-ct-report/atlassian-proxy", max-age=86400
Strict-Transport-Security: max-age=63072000; preload
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Atl-Traceid: 67df1d34accea832
Report-To: {"group": "endpoint-1", "max_age": 600, "endpoints": [{"url": "https://treter.cloudfront.net"}], "include_subdomains": true}
Nel: {"report_to": "endpoint-1", "max_age": 600, "include_subdomains": true, "failure_fraction": 0.001}
Content-Type: application/octet-stream
Content-Length: 0
}}

 

And when i remove this line ( content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); ) of code then i receive below error 

 




{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Wed, 10 Aug 2022 09:17:17 GMT
Server: globaledge-envoy
Atl-Confluence-Via: h:confluence-prod-ap-1-1.prod.atl-paas.net
Cache-Control: no-store, must-revalidate, no-cache
X-Envoy-Upstream-Service-Time: 335
Expect-Ct: report-uri="https://web-security-reports.services.atlassian.com/expect-ct-report/atlassian-proxy", max-age=86400
Strict-Transport-Security: max-age=63072000; preload
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Atl-Traceid: 124d75590eccbd19
Report-To: {"group": "endpoint-1", "max_age": 600, "endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "include_subdomains": true}
Nel: {"report_to": "endpoint-1", "max_age": 600, "include_subdomains": true, "failure_fraction": 0.001}
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: application/json
Expires: Thu, 01 Jan 1970 00:00:00 GMT
}}

{"statusCode":500,"message":"java.lang.RuntimeException: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/octet-stream"}

 

 

Any help would be highly appreciated. 

 

Thank you!

Aman

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events