Page tree
Skip to end of metadata
Go to start of metadata

Simplest call

List the first 25 spaces
result.promise = httpClient.get('/rest/api/space');
 Click here to see the result...
List the first 25 spaces
{
  "results": [
    {
      "id": 688129,
      "key": "DOC",
      "name": "Documentation",
      "type": "global",
      "_links": {
        "self": "https://cloudscript.atlassian.net/wiki/rest/api/space/DOC"
      },
      "_expandable": {
        "icon": "",
        "description": "",
        "homepage": "/rest/api/content/491524"
      }
    },{"...": "..."},{"...": "..."}
  ],
  "start": 0,
  "limit": 25,
  "size": 3,
  "_links": {
    "self": "https://cloudscript.atlassian.net/wiki/rest/api/space",
    "base": "https://cloudscript.atlassian.net/wiki",
    "context": "/wiki"
  }
}

 

 

First then

This one liner allows to retrieve the first 25 spaces of your Confluence instance. This will return the raw JSON returned by the Confluence REST API. To manipulate the result you can chain that with a `then()` call.

List the name of the first 25 spaces
result.promise = httpClient.get('/rest/api/space').then(function (body) {
  var spaces = JSON.parse(body).results;
  var output = [];
  var i = 0;
  for(i=0; i< spaces.length; i++) {
 	output.push(spaces[i].name);
  }
  console.log(output.join(","));
});
List the first 25 spaces
{
  "status": 200,
  "text": "Documentation,Example space,SandBox\n"
}

 

 

 

Choosing the conten-type

If you return a json object, with a body property you can override the content-type header.

List the name of the first 25 spaces
result.promise = httpClient.get('/rest/api/space').then(function (body) {
  var spaces = JSON.parse(body).results;
  var output = [];
  var i = 0;
  for(i=0; i< spaces.length; i++) {
 	output.push(spaces[i].name);
  }
  return {body: output.join(","), "content-type": "text/plain"};
});
List the first 25 spaces
Documentation,Example space,SandBox

 

Merging result object

If an object is returned without a body property, it will be merged with the usual result.

List the name of the first 25 spaces
result.promise = httpClient.get('/rest/api/space').then(function (body) {
  var spaces = JSON.parse(body).results;
  var output = [];
  var i = 0;
  for(i=0; i< spaces.length; i++) {
 	output.push(spaces[i].name);
  }
  return {names: output};
});
List the first 25 spaces
{
  "names": [
    "Documentation",
    "Example space",
    "SandBox"
  ],
  "status": 200,
  "console": ""
}

REST call on an array

Now that we have a list of spaces, we can get fancy about what we do with it, for example retrieve the user who last updated the home page of each space:

var Promise = require("bluebird");
result.promise = httpClient.get('/rest/api/space').then(function (body) {
	var spaces = JSON.parse(body).results;
	var p = [];
	var i = 0;
	for (i = 0; i < spaces.length; i++) {
		var homepageUrl = spaces[i]._expandable.homepage;
		p.push(httpClient.get(homepageUrl).then(function (homepageBody) {
			var homepage = JSON.parse(homepageBody);
			return {
				name: homepage.space.name,
				createdBy: homepage.history.createdBy.displayName
			};
		}));
	}
	return Promise.all(p);
});
[
  {
    "name": "Documentation",
    "createdBy": "Administrator"
  },
  {
    "name": "Example space",
    "createdBy": "CloudScript"
  },
  {
    "name": "SandBox",
    "createdBy": "Patrick @Cloudscript"
  }
]

More than 25 result

By default the Atlassian API are limited to 25 items per call, what if you need to go through all the results?

 
 
  • No labels
Write a comment…