Batch API¶
The Batch API lets you combine multiple REST API requests into a single HTTP request. This is highly efficient for performing multiple operations while minimizing network overhead and latency.
Understand batch requests¶
A batch request consists of multiple individual sub-requests. ServiceNow executes each sub-request and returns the results in a single combined response.
Key benefits¶
- Reduced Latency: Fewer round-trips between your application and ServiceNow.
- Improved Performance: Faster execution of multiple related tasks.
- Efficiency: Better utilization of network resources.
Create a batch request¶
To create a batch request, you first need to define the individual requests
using the SDK's request builders and then combine them into a
BatchRequestModel.
// Step 3. Build requests, using ToXXXRequestInformation method
body, err := batchRequests(true, requests...)
if err != nil {
log.Fatal(err)
}
// Step 4: Configure request
postConfig := &batchapi.BatchRequestBuilderPostRequestConfiguration{}
// Step 5: Execute request
batch_response, err := client.Now2().Batch().Post(context.Background(), body, postConfig)
if err != nil {
log.Fatal(err)
}
Batch helper¶
Combining multiple requests can involve repetitive code. You can use a helper function to streamline the process of adding requests to your batch model.
// batchRequests A helper function to combine provided request information into a single `BatchRequest`.
func batchRequests(excludeResponseHeaders bool, requests ...*abstractions.RequestInformation) (*batchapi.BatchRequestModel, error) {
body := batchapi.NewBatchRequestModel()
for _, request := range requests {
restRequest, err := batchapi.CreateRestRequestFromRequestInformation(request, excludeResponseHeaders)
if err != nil {
return nil, err
}
if err := body.AddRequest(restRequest); err != nil {
return nil, err
}
}
return body, nil
}
Next steps¶
- Table Operations: Learn about the types of table requests you can batch.
- Attachments: Learn about managing attachments, which can also be part of a batch request.