Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating HTTP POST request for creating Confluence Pages: 403 Error

Caleb Valerian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 24, 2022

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

1 answer

0 votes
Fabio Racobaldo _Catworkx_
Community Champion
October 25, 2022
Caleb Valerian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 25, 2022

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);

Suggest an answer

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

Atlassian Community Events