Skip to end of metadata
Go to start of metadata

Since SonarQube 6.0

All ajax requests must provide CSRF protection token. In order to help you to do so, we provide a set of useful helpers.

Getting Started

Let's start with a simple GET request

window.SonarRequest
    .getJSON('/api/issues/search')
    .then(function (response) {
      // here 'response' contains the object representing the JSON output
    });
  • window.SonarRequest contains all the helper methods to do an API requests.
  • window.SonarRequest.getJSON is a simplest helper to do an API call, receive some data and parse it as JSON.

API Documentation

window.SonarRequest.request(url: string): Request

Start making an API call. Return a Request instance which has the following methods:

  • setMethod(method: string): Request sets the http method, can be GET, POST, etc.
  • setData(data: object): Request sets the request parameters`
  • submit(): Promise sends the request

window.SonarRequest.getJSON(url: string[, data: object]): Promise

Send a GET request, get a response, parse it as JSON.

window.SonarRequest.postJSON(url: string[, data: object]): Promise

Send a POST request, get a response, parse it as JSON.

window.SonarRequest.post(url: string[, data: object]): Promise

Send a POST request, ignore the response content.

More Examples

Get the list of unresolved issues

window.SonarRequest.getJSON(
  '/api/issues/search',
  { resolved: false }
).then(function (response) {
  // response.issues contains the list of issues
});

Create new project

window.SonarRequest.post(
  '/api/projects/create',
  { key: 'sample', name: 'Sample' }
).then(function () {
  // the project has been created
});

Handle bad requests

window.SonarRequest.post(
  '/api/users/deactivate',
  { login: 'admin' }
).catch(function (error) {
  // error.response.status === 400
  // error.response.statusText === 'Bad Request'
  // To read the response:
  // error.response.json().then(function (jsonResponse) { ... });
});
  • No labels