batch

To execute several methods in one request, use the core/batch method.

Endpoint

The request can be sent in one of two forms. In the first form, the params parameter contains an array of methods:

Copied!
svc=core/batch&params=[
  {
    "svc": "<text>",
    "params": {}
  }
]

In the second form, the params parameter contains an object, which also allows you to use the flags parameter:

Copied!
svc=core/batch&params={
  "params": [{
    "svc": "<text>",
    "params": {}
  }],
  "flags": <uint>
}

Parameters

The svc and params parameters are specified for each method of the array. The flags parameter is specified for the request as a whole.

Name Description
svc The name of the method to execute, for example, core/search_items.
params The parameters of the method.
flags A bitmask that can include the following flags:
  • 0x01 (1): stop the request if one of the methods returns an error.
  • 0x02 (2): apply the limits on the number of methods and the response size. For details, see Limits. This flag isn’t available in Wialon Local.
Specify 0 to execute all the methods without these limits. The parameter is available only in the second form of the request. If you omit it in this form, the request returns error 4.

Returned result

If the request is completed successfully, the result is returned in the following format:

Copied!
[
  { /* Object with data if there are no errors. */
    ...
  },
  { /* Error code if an error has occurred. */
    "error": <int>
  },
  ...
]

Each element of the array is the result of the corresponding method, without an additional wrapper. If a method returns an error, its element contains the error code of this method.

If the flags parameter includes the 0x01 (1) flag and one of the methods returns an error, all the methods that follow it return error code 10.

Limits

If the flags parameter includes the 0x02 (2) flag, the following limits apply to the request. The values in the table are the default ones; the server can use other values.

Limit Default value Result if the limit is exceeded
Number of methods in the request 100 The methods aren’t executed, and the request returns error 4.
Size of the result of one method 1 MB The element of this method contains {"error":6,"reason":"BATCH_SINGLE_RESPONSE_TOO_LARGE"}.
Size of the whole response 10 MB The element of the method whose result exceeds the limit contains {"error":6,"reason":"BATCH_RESPONSE_TOO_LARGE"}.

If one of the size limits is exceeded, the methods that follow aren’t executed and aren’t included in the response, so the array can contain fewer elements than the request. If the element with the error doesn’t fit into the response either, the array ends with the last element that fits.

Error codes

The request itself can return the following error codes:

Code Description
4 Wrong input parameters. This happens in one of the following cases:
  • the request doesn’t match either of the two forms, for example, the second form doesn’t contain the flags parameter. In this case, the response has no reason field
  • one of the elements of the array isn’t a valid method. In this case, the reason field contains BATCH_INVALID_INTERNAL_REQUEST index=<i>, where <i> is the position of this element in the array, starting from 0
  • the flags parameter includes the 0x02 (2) flag, and the request contains more methods than allowed. In this case, the reason field contains BATCH_MAX_INTERNAL_REQUESTS_EXCEEDED count=<n> max=<max>, where <n> is the number of methods in the request and <max> is the limit
6 The returned result is too large. This happens in one of the following cases:
  • the flags parameter doesn’t include the 0x02 (2) flag, and the response can’t be built because of its size. In this case, the reason field contains Large response
  • the flags parameter includes the 0x02 (2) flag, and the built response exceeds the limit on the size of the whole response. In this case, the reason field contains BATCH_RESPONSE_TOO_LARGE size=<size> max=<max>, where <size> is the size of the response in bytes and <max> is the limit

A separate method can return its own error code in its element of the array. Apart from the codes of the method itself, the following codes are possible:

Code Description
2 Wrong method name in the svc parameter.
6 The result is too large. This code is possible only if the flags parameter includes the 0x02 (2) flag. For details, see Limits.
10 The method isn’t executed. This happens in one of the following cases:
  • one of the previous methods has returned an error and the flags parameter includes the 0x01 (1) flag
  • the request has exceeded the time limit set on the server

Examples

All the examples below send the same two methods: the first one refers to the item with ID 1004, to which the user has no access rights, and the second one gets the properties of the resource with ID 1003. Add the sid parameter with your session identifier to each request.

Executing all the methods despite an error

In the first form, the params parameter contains only the array of methods, so there’s nowhere to specify the flags parameter. All the methods are executed.

Copied!
svc=core/batch&params=[
  {
    "svc": "core/search_item",
    "params": {
      "id": 1004,
      "flags": 1
    }
  },
  {
    "svc": "core/search_item",
    "params": {
      "id": 1003,
      "flags": 1
    }
  }
]

The second form gives the same result if the value of the flags parameter is 0.

Copied!
svc=core/batch&params={
  "params": [
    {
      "svc": "core/search_item",
      "params": {
        "id": 1004,
        "flags": 1
      }
    },
    {
      "svc": "core/search_item",
      "params": {
        "id": 1003,
        "flags": 1
      }
    }
  ],
  "flags": 0
}

Response:

Copied!
[
  {
    "error": 7
  },
  {
    "item": {
      "nm": "Resource",
      "cls": 3,
      "id": 1003,
      "mu": 0,
      "uacl": 19826928009763
    },
    "flags": 1
  }
]

The error of the first method doesn’t affect the second one.

Stopping the request after the first error

To stop the execution after the first error, use the second form of the request and specify the 0x01 (1) flag. The methods are the same, but now the second one isn’t executed and returns error 10.

Copied!
svc=core/batch&params={
  "params": [
    {
      "svc": "core/search_item",
      "params": {
        "id": 1004,
        "flags": 1
      }
    },
    {
      "svc": "core/search_item",
      "params": {
        "id": 1003,
        "flags": 1
      }
    }
  ],
  "flags": 1
}

Response:

Copied!
[
  {
    "error": 7
  },
  {
    "error": 10
  }
]

If you find a mistake in the text, please select it and press Ctrl+Enter.