Forums

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

500 error when trying to create page using REST API

Laurence November 10, 2021

I'm currently using Confluence server and I'm running into this error when I try to create a new page using the REST API. I am currently using an HTML macro that makes GET & POST requests using the Fetch API. I currently have no issues when making GET requests, only issues with POST requests. 



function createPage() {
let url = "http://exampledomain:8090/confluence/rest/api/content/"

    fetch(url, {

        method: 'POST',

        headers: {

            'Content-Type': 'application/json',

            mode: 'cors',

            cache: 'no-cache',

            credentials: 'same-origin',

        },

        data: {

            'type': 'page',

            'title': "New Page",

            'ancestors': [{ 'id': 97059352 }], // parent page id

            'space': { 'key': "EXAMPLE_SPACE" },

            'body': {

                'storage': {

                    'value': "<h1>New Page!</h1>",

                    'representation': 'storage',

                }

            },

        }

    })

.then(response => console.log(response))

.catch(err => console.log(err.message))
}

1 answer

0 votes
Thomas Deiler
Community Champion
November 10, 2021

Dear @Laurence ,

try to set following headers:

Accept: application/json
User-Agent: x

So long

Thomas

Laurence November 10, 2021

EDIT: Now it's a 500 error, no longer that original error 

 

Thanks for the reply.

 

I tried adding those headers, but still no luck. I did just realize that I probably need to pass an Authorization header in the format, which I just did, but still not working.

 

I tried submitting the requests with certain headers commented out as well with different combinations, but still nothings working.

 

I was referring to this tutorial for which headers to pass: https://pretagteam.com/question/create-a-confluence-page-using-python-api


function createPage() {
let url = "http://exampledomain:8090/confluence/rest/api/content/"

    fetch(url, {

        method: 'POST',

        headers: {
'Authorization': 'Basic USER:PASS',

            'Content-Type': 'application/json',

'Accept' 'application/json',

'User-Agent': 'x',

            // mode: 'cors',

           // cache: 'no-cache',

            // credentials: 'same-origin',

        },

        data: {

            'type': 'page',

            'title': "New Page",

            'ancestors': [{ 'id': 97059352 }], // parent page id

            'space': { 'key': "EXAMPLE_SPACE" },

            'body': {

                'storage': {

                    'value': "<h1>New Page!</h1>",

                    'representation': 'storage',

                }

            },

        }

    })

.then(response => console.log(response))

.catch(err => console.log(err.message))
}

 

Thomas Deiler
Community Champion
November 12, 2021

The authorization header will not work like that, because you need to encrypt.

Try this link.

I recommend to use a browser REST extension, to build and test your POST rest call, first. If it works, then you can more or less "Copy & Paste" to python.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events