Bad Request - Create Confluence Content Page

Hi,

I am trying to create a new page under a space using the following code in nodejs:

var jsondata = { "type": "page", "title": "My Test Page", "space": { "key": "TEST" }, "body": { "wiki": { "value": "h1. Hello world!", "representation": "wiki" } }};

router.get('/content', (req, res) => {
 axios.post(`${confluenceRootUri}/rest/api/content`, {
    headers: { 
        'Content-Type': 'application/json; charset=utf-8',
        "Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64')
    },
    data: JSON.stringify(jsondata)
 }) 
 .then(response => { 
    res.send(response.data); 
 }) 
 .catch(error => { 
    res.status(error.status || 500); 
    res.send(error.response.data); 
 });
}

However I am getting the following bad request:

{
    statusCode: 400,
    data: {
      authorized: true,
      valid: false,
      allowedInReadOnlyMode: true,
      errors: [
        {
           message: {
             key: "type is required to create content",
             args: [ ]
           }
        }
      ],
      successful: false
    },
    message: "Could not create content with type null",
    reason: "Bad Request"
}

I also have tried the example from the website in python but it gives me syntax error

curl -u admin:admin -X POST -H 'Content-Type: application/json' -d '{"type":"page","title":"new page",
"space":{"key":"TST"},"body":{"storage":{"value":"<p>This is <br/> a new page</p>","representation": 
"storage"}}}' http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool

What am I missing?

Welcome to the Developer Community, @renatogbp!

I tried this exact script (by copy-paste) via CLI, only changing the port, and it works for me. Can you paste the error you got for this curl command?

Cheers,
Ian

This is what I get. I tried with my server URL, username and password but still got the same error.

However, what I really want to know is why my nodejs script doesn’t work, since I set the “type”: “page”, and it gives me “type is required”

Okay, I found my problem, my axios request was in the wrong format, the corrected code is:

router.get('/content', (req, res) => {
    if (req.query.hasOwnProperty("name")) {
        jsondata.title = req.query.name;
        axios({
            method: 'post',
            url: `${confluenceRootUri}/rest/api/content`,
            headers: { 
                'Content-Type': 'application/json; charset=utf-8',
                "Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64') 
            },
            data: jsondata
        })
        .then(response => {
            res.send(response.data);
        })
        .catch(error => {
            res.status(error.status || 500);
            res.send(error.response.data);
        });
    }
    else {
        res.send("Content name invalid");
    }
})
1 Like