500 error when trying to create page using REST API
Asked by Laurence:

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))
}
```


Answers:
author: Thomas Deiler
body: Dear [@Laurence](/t5/user/viewprofilepage/user-id/4625142) ,

try to set following headers:

```
Accept: application/json
User-Agent: x
```

So long

Thomas

comments: author: Laurence
body: **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))
}
```


author: Thomas Deiler
body: The authorization header will not work like that, because you need to encrypt.

Try this [link](https://stackoverflow.com/questions/52469747/python-rest-post-with-basic-auth).

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.


Tags: confluence-server

server