Documentation

request

The Request Javascript module provides a mechanism for an add-on rendered in an iframe to make an XMLHttpRequest to the host product without requiring CORS.

In contrast to REST calls made from the add-on server to the product directly, any requests made in the browser are evaluated in the context of the currently logged in user. The requested resource is still evaluated against the add-ons granted scopes.

Example

AP.request('../assets/js/rest-example.json', {
  success: function(responseText){
    alert(responseText);
  }
});

Methods

request(url, options)

Execute an XMLHttpRequest in the context of the host application. The format of the response (dataType) will always be set to "text" - even if specified.

Parameters:
Name Type Description
url String

Either the URI to request or an options object (as below) containing at least a 'url' property;
This value should be relative to the context path of the host application.

options Object

The options of the request.

Properties
Name Type Default Required Description
url String Required

The url to request from the host application, relative to the host's context path

type String GET

The HTTP method name.

cache Boolean true

If the request should be cached.

data String | Object

The body of the request; required if type is 'POST' or 'PUT'

contentType String

The content-type string value of the entity body, above; required when data is supplied.

headers Object

An object containing headers to set; supported headers are: 'Accept', 'If-Match' and 'If-None-Match'.

success function

A callback function executed on a 200 success status code.

error function

A callback function executed when a HTTP status error code is returned.

experimental Boolean false

If this is set to true, the developer acknowledges that the API endpoint which is being called may be in beta state, and thus may also have a shorter deprecation cycle than stable APIs.

Examples
// A simple POST request which logs response in the console.
AP.request({
  url: '../assets/js/rest-example.json',
  type: 'POST',
  data: {name: 'some text', description: 'test'}
  success: function(responseText){
    console.log(responseText);
  },
  error: function(xhr, statusText, errorThrown){
    console.log(arguments);
  }
});
// Upload an attachment to a Confluence entity.
var fileToUpload = document.getElementById("fileInput").files[0];

AP.request({
  url: '/rest/api/content/123456/child/attachment',
  type: 'POST',
  contentType: 'multipart/form-data',
  data: {comment: 'example comment', file: fileToUpload},
  success: function(responseText){
    alert(responseText);
  }
});