Project: APISIX (Orchestration & Management / API Gateway)
File: batch-requests.md

title: batch-requests keywords: Apache APISIX API Gateway Plugin Batch Requests description: This document contains information about the Apache APISIX batch-request Plugin. <!-- --> After enabling the batch-requests plugin, users can assemble multiple requests into one request and send them to the gateway. The gateway will parse the corresponding requests from the request body and then individually encapsulate them into separate requests. Instead of the user initiating multiple HTTP requests to the gateway, the gateway will use the HTTP pipeline method, go through several stages such as route matching, forwarding to the corresponding upstream, and then return the combined results to the client after merging. In cases where the client needs to access multiple APIs, this will significantly improve performance. :::note The request headers in the users original request (except for headers starting with Content-, such as Content-Type) will be assigned to each request in the HTTP pipeline. Therefore, to the gateway, these HTTP pipeline requests sent to itself are no different from external requests initiated directly by users. They can only access pre-configured routes and will undergo a complete authentication process, so there are no security issues. If the request headers of the original request conflict with those configured in the plugin, the request headers configured in the plugin will take precedence (except for the realipheader specified in the configuration file). ::: None. This plugin adds `/apisix/batch-requests` as an endpoint. :::note You may need to use the plugin to expose this endpoint. ::: You can enable the `batch-requests` Plugin by adding it to your configuration file (`conf/config.yaml`): ```yaml title="conf/config.yaml" plugins: ... batch-requests ``` By default, the maximum body size that can be sent to `/apisix/batch-requests` can't be larger than 1 MiB. You can change this configuration of the Plugin through the endpoint `apisix/admin/plugin_metadata/batch-requests`: :::note You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: ```bash adminkey=$(yq '.deployment.admin.adminkey[0].key' conf/config.yaml | sed 's/"//g') ``` ::: ```shell curl http://127.0.0.1:9180/apisix/admin/pluginmetadata/batch-requests -H "X-API-KEY: $adminkey" -X PUT -d ' { "maxbodysize": 4194304 }' ``` | Name | Type | Required | Default | Valid values | Description | | - | - | -- | - | | | | maxbodysize | integer | True | 1048576 | [1, ...] | Maximum size of the request body in bytes. | This plugin will create an API endpoint in APISIX to handle batch requests. | Name | Type | Required | Default | Description | | -- || -- | - | -- | | query | object | False | | Query string for the request. | | headers | object | False | | Headers for all the requests. | | timeout | integer | False | 30000 | Timeout in

| | pipeline | array] | True | | Details of the request. | | Name | Type | Required | Default | Valid | Description | | - | - | -- | - | -- | - | | version | string | False | 1.1 | [1.0, 1.1] | HTTP version. | | method | string | False | GET | ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE"] | HTTP method. | | query | object | False | | | Query string for the request. If set, overrides the value of the global query string. | | headers | object | False | | | Headers for the request. If set, overrides the value of the global query string. | | path | string | True | | | Path of the HTTP request. | | body | string | False | | | Body of the HTTP request. | | ssl_verify | boolean | False | false | | Set to verify if the SSL certs matches the hostname. | The response is an array of . | Name | Type | Description | | - | - | - | | status | integer | HTTP status code. | | reason | string | HTTP reason-phrase. | | body | string | HTTP response body. | | headers | object | HTTP response headers. | You can specify a custom URI with the Plugin. You can set the URI you want when creating the Route and change the configuration of the public-api Plugin: ```shell curl http://127.0.0.1:9180/apisix/admin/routes/br -H "X-API-KEY: $admin_key" -X PUT -d ' { "uri": "/batch-requests", "plugins": { "public-api": { "uri": "/apisix/batch-requests" } } }' ``` First, you need to setup a Route to the batch request API. We will use the Plugin for this: ```shell curl http://127.0.0.1:9180/apisix/admin/routes/br -H "X-API-KEY: $admin_key" -X PUT -d ' { "uri": "/apisix/batch-requests", "plugins": { "public-api": {} } }' ``` Now you can make a request to the batch request API (`/apisix/batch-requests`): ```shell curl --location --request POST 'http://127.0.0.1:9080/apisix/batch-requests' \ --header 'Content-Type: application/json' \ --data '{ "headers": { "Content-Type": "application/json", "admin-jwt":"xxxx" }, "timeout": 500, "pipeline": [ { "method": "POST", "path": "/community.GiftSrv/GetGifts", "body": "test" }, { "method": "POST", "path": "/community.GiftSrv/GetGifts", "body": "test2" } ] }' ``` This will give a response: ```json [ { "status": 200, "reason": "OK", "body": "{\"ret\":500,\"msg\":\"error\",\"gameinfo\":null,\"gift\":[],\"togets\":0,\"getallmsg\":\"\"}", "headers": { "Connection": "keep-alive", "Date": "Sat, 11 Apr 2020 17:53:20 GMT", "Content-Type": "application/json", "Content-Length": "81", "Server": "APISIX web server" } }, { "status": 200, "reason": "OK", "body": "{\"ret\":500,\"msg\":\"error\",\"gameinfo\":null,\"gift\":[],\"togets\":0,\"getallmsg\":\"\"}", "headers": { "Connection": "keep-alive", "Date": "Sat, 11 Apr 2020 17:53:20 GMT", "Content-Type": "application/json", "Content-Length": "81", "Server": "APISIX web server" } } ] ``` You can remove `batch-requests` from your list of Plugins in your configuration file (`conf/config.yaml`).