Search code examples
google-apps-scriptconfluence-rest-api

Google Apps Script to Create Confluence Page -- Results in Status Code 500


Note: I also posted this question on the Atlassian forum here: https://community.atlassian.com/t5/Confluence-questions/Google-Apps-Script-to-Create-Confluence-Page-HTTP-500-Error/qaq-p/1039040#M66094

I'm reaching out to a larger audience here on SO.

I'm using the following google apps script code in a Google Sheet to create a Confluence page:

headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + 
pswd), "Content-Type": "application/json"};

url = "https://confluence.asdf.com/rest/api/content/";
var params = { 
  "method":"POST",
  "headers":headers,
  "muteHttpExceptions": false,
  "type":"page",
  "title":newTitle,
  "space":{"key":"DOC"},
  "body":{"storage":{"value": "<p>This is <br/> a new page</p>" }, 
  "representation":"storage"} 
};
var createResponse = UrlFetchApp.fetch(url, params); 

However, when I submit the request, I receive this response:

Request failed for https://confluence.atlas.asdf.com/rest/api/content/ 
returned code 500.
Truncated server response: {"statusCode":500,"
message":"<?> No content to map to Object due to end of 
input","reason":"Internal Server Error"} (use muteHttpExceptions option to 
examine full response)

I realize there are curl samples out there which I've seen but do not help me. What am I doing wrong?

Edit: 25-March @Tanaike I modified the code as you suggested:

    headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + pswd) };


    var payload = {
      "type": "page",
      "title": newTitle,
      "space": {"key": "DOC"},
      "body": {
        "storage": {
          "value": "<p>This is <br/> a new page</p>"
        },
        "representation": "storage"
      }
    };
    var params = { 
      "method": "POST",
      "headers": headers,
      "muteHttpExceptions": false,
      "payload": JSON.stringify(payload),
      "contentType": "application/json"
    };

    var createResponse = UrlFetchApp.fetch(url, params);        

I receive the same error as before.


Solution

  • How about this modification?

    Modified script:

    Please modify params as follows and test again. From the error message in your question, the request body was put in the property of payload.

    var payload = {
      "type": "page",
      "title": newTitle,
      "space": {"key": "DOC"},
      "body": {
        "storage": {
          "value": "<p>This is <br/> a new page</p>"
        },
        "representation": "storage"
      }
    };
    var params = { 
      "method": "POST",
      "headers": headers,
      "muteHttpExceptions": false,
      "payload": JSON.stringify(payload)
    };
    

    Note:

    • This modification supposes that each value in payload and headers is correct.
    • "Content-Type": "application/json" in headers can be also put in params as "contentType": "application/json".

    References:

    I cannot test this modification. So if this didn't work, can you provide the error message? I would like to think of about the issue.

    Edit:

    From the official document, it seems that the property of body is "body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}. So please modify as follows.

    Modified script:

    headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + pswd) };
    var payload = {
      "type": "page",
      "title": newTitle,
      "space": {"key": "DOC"},
      "body": {
        "storage": {
          "value": "<p>This is <br/> a new page</p>",
          "representation": "storage" // Modified
        }
      }
    };
    var params = { 
      "method": "POST",
      "headers": headers,
      "muteHttpExceptions": false,
      "payload": JSON.stringify(payload),
      "contentType": "application/json"
    };
    var createResponse = UrlFetchApp.fetch(url, params);
    

    Note:

    • In my environment, I could confirm that the result was the same with the result from the official sample script. If this didn't work, please confirm each value of payload and headers, again.