EDIT: I've been tinkering away with the HTTP Request and I've gotten it to now send my a 403 Forbidden error. I believe something is wrong with my Authorization header.
New JSON (validated through JSONLint)
string postContent = "{\"type\":\"page\",\"title\":\""
+ htmlTitle + "\",\"ancestors\":[{\"id\":"
+ ancestorID + "}],\"space\":{\"key\":\""
+ spaceKey + "\"},\"body\":{\"storage\":{\"value\":\""
+ htmlBody + "\",\"representation\":\"storage\"}}}";
Authorization Header: I've tried both Base64 Encryption and non-encrypted and get the same 403 error.
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://bigfishgames.atlassian.net/wiki/rest/api/content/"))
{
// Encrypted Header
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes(confluenceEmail + ":" + confluenceToken));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
// Non-Encrypted Header
request.Headers.TryAddWithoutValidation("Authorization", confluenceEmail + ":"+ confluenceToken);
request.Content = new StringContent(postContent);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
}
}
====================================
Im working on a script to automatically upload documents to Confluence using the REST API. The problem I'm having is converting the CURL command into a working HTTP POST Request. I've tried several different approaches to making a working HTTP request but keep getting Server Error 500.
// Async HTTP POST Request function to upload the documents to Confluence via REST API
public static async Task postHTTP(string htmlTitle, string htmlBody, string logFile, string confluenceEmail, string confluenceToken, string spaceKey, string ancestorID)
{
string postContent = "{\"type\":\"page\",\"title\":\""
+ htmlTitle + "\"ancestors\":[{\"id\":"
+ ancestorID + "}],\"space\":{\"key\":\""
+ spaceKey + "\"},\"body\":{\"storage\":{\"value\":\""
+ htmlBody + "\",\"representation\":\n\"storage\"}}}";
// In production code, don't destroy the HttpClient through using, but better use IHttpClientFactory factory or at least reuse an existing HttpClient instance
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests
// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://bigfishgames.atlassian.net/wiki/rest/api/content/"))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes(confluenceEmail + ":" + confluenceToken));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent(postContent);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
File.AppendAllText(logFile, response.ToString());
}
}
}
Does anyone know how to make a working HTTP POST call from C#? I've checked several sources from StackOverflow and the forums but haven't been able to get a working solution yet.
Thanks
Hi @Caleb Valerian ,
did u try using the following examples https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content/#api-wiki-rest-api-content-post
Fabio
Hey @Fabio Racobaldo _Catworkx_
I have looked at the linked pages, and I tried to follow the Python example but convert it to C# valid code is the challenge I'm experiencing. This is another take at trying to get the call to go through with the WebRequest
public void HTTPRequest(string htmlTitle, string htmlBody, string logFile, string confluenceEmail, string confluenceToken, string spaceKey, string ancestorID)
{
string postContent = "{\"type\":\"page\",\"title\":\""
+ htmlTitle + "\"ancestors\":[{\"id\":"
+ ancestorID + "}],\"space\":{\"key\":\""
+ spaceKey + "\"},\"body\":{\"storage\":{\"value\":\""
+ htmlBody + "\",\"representation\":\n\"storage\"}}}";
WebRequest myReq = WebRequest.Create("https://bigfishgames.atlassian.net/wiki/rest/api/content/");
myReq.Method = "POST";
myReq.ContentLength = htmlBody.Length;
myReq.ContentType = "application/json; charset=UTF-8";
UTF8Encoding enc = new UTF8Encoding();
myReq.Headers.Add("auth-token", confluenceEmail + ":" + confluenceToken);
int count = enc.GetBytes(postContent).Length;
using (Stream ds = myReq.GetRequestStream())
{
ds.Write(enc.GetBytes(postContent), 0, count);
}
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
File.AppendAllText(logFile, content);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.