Confluence REST API POST request returning "500 Internal Server Error"

I’m currently using Confluence server and I’m currently getting a 500 error when I try to create a new child 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: {
       'Authorization': 'Basic USER:PASS',
       'Content-Type': 'application/json',
    },
    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))
}

Hi @Laurence - is that just pseudo-code for your authorization header? I’m asking because plain text user:pass is not what should be passed there. Instead, it needs to be signed with base64. You can find tools online, or give it a whirl on the command line (if you’re on a *nix based OS):

$ echo -n ‘foo@bar.com:myPassword’ | base64

If that’s not the issue, is there any more verbose error info that Confluence is returning?

2 Likes

Hey @nmansilla,

I tried signing the username and password with base64 using the btoa() method, but still no luck. Here is the updated code, any ideas of why I’m still getting a 500 error?

let createPage = function () {
   let url = "http://exampledomain:8090/confluence/rest/api/content/"
   let username = btoa("username")
   let password = btoa("password")
   fetch(url, {
       method: 'POST',
       headers: {
           'Authorization': `Basic ${username}:${password}`,
           'Content-Type': 'application/json'
       },
       data: {
           'type': 'page',
           'title': "New Page",
           'ancestors': [{ 'id': 97059347 }],
           'space': { 'key': "SPACE_KEY" },
           'body': {
               'storage': {
                   'value': "<h1>New Page!</h1>",
                   'representation': 'storage',
               }
           },
       }
   })
.then(response => console.log(response))
.catch(err => console.log(err.message))
}

Hi @Laurence

try replace

'Authorization': `Basic ${username}:${password}`,

to

'Authorization': 'Basic ' + btoa(username + ":" + password), 

where username and password are not encoded values

Example: user “admin” with password “admin1234”)

'Authorization': 'Basic ' + btoa("admin:admin1234"), 

Cheers
Adam

Thanks for the response,

Unfortunately that doesn’t seem to be working either.

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

    fetch(url, {
        method: "POST",
        headers: {
			"Authorization": "Basic " + btoa(username + ":" + password), 
            "Content-Type": "application/json"
        },
        data: {
            "type": "page",
            "title": "New Page",
            "ancestors": [{ "id": 97059347 }],
            "space": { "key": "SPACE_KEY" },
            "body": {
                "storage": {
                    "value": "<h1>New Page!</h1>",
                    "representation": "storage"
                }
            }
        }
    })
        .then(response => console.log(response))
        .catch(err => console.log(err.message))
}

Hi @Laurence

try this:

let createPage = function () {
    let url = "http://localhost:1990/confluence/rest/api/content"
    let username = "admin"
    let password = "admin"

    fetch(url, {
        method: "POST",
        headers: {
			"Authorization": "Basic " + btoa(username + ":" + password), 
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            "type": "page",
            "title": "New Page",
            "ancestors": [{ "id": 786434 }],
            "space": { "key": "AAA" },
            "body": {
                "storage": {
                    "value": "<h1>New Page!</h1>",
                    "representation": "storage"
                }
            }
        })
    })
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log('error', error));
}; 

createPage();

Cheers
Adam

2 Likes

Thank you! It’s finally working now

(post deleted by author)