Title: WP_REST_Server::serve_batch_request_v1
Published: December 9, 2020
Last modified: August 20, 2026

---

# WP_REST_Server::serve_batch_request_v1( WP_REST_Request $batch_request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#changelog)

Serves the batch/v1 request.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#parameters)󠁿

 `$batch_request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

The batch request object.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
The generated response object.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#source)󠁿

    ```php
    public function serve_batch_request_v1( WP_REST_Request $batch_request ) {
    	$requests = array();

    	foreach ( $batch_request['requests'] as $args ) {
    		$parsed_url = wp_parse_url( $args['path'] );

    		if ( false === $parsed_url ) {
    			$requests[] = new WP_Error( 'parse_path_failed', __( 'Could not parse the path.' ), array( 'status' => 400 ) );

    			continue;
    		}

    		$single_request = new WP_REST_Request( $args['method'] ?? 'POST', $parsed_url['path'] );

    		if ( ! empty( $parsed_url['query'] ) ) {
    			$query_args = array();
    			wp_parse_str( $parsed_url['query'], $query_args );
    			$single_request->set_query_params( $query_args );
    		}

    		if ( ! empty( $args['body'] ) ) {
    			$single_request->set_body_params( $args['body'] );
    		}

    		if ( ! empty( $args['headers'] ) ) {
    			$single_request->set_headers( $args['headers'] );
    		}

    		$requests[] = $single_request;
    	}

    	$matches    = array();
    	$validation = array();
    	$has_error  = false;

    	foreach ( $requests as $single_request ) {
    		if ( is_wp_error( $single_request ) ) {
    			$has_error    = true;
    			$matches[]    = $single_request;
    			$validation[] = $single_request;
    			continue;
    		}

    		$match     = $this->match_request_to_handler( $single_request );
    		$matches[] = $match;
    		$error     = null;

    		if ( is_wp_error( $match ) ) {
    			$error = $match;
    		}

    		if ( ! $error ) {
    			list( $route, $handler ) = $match;

    			if ( isset( $handler['allow_batch'] ) ) {
    				$allow_batch = $handler['allow_batch'];
    			} else {
    				$route_options = $this->get_route_options( $route );
    				$allow_batch   = $route_options['allow_batch'] ?? false;
    			}

    			if ( ! is_array( $allow_batch ) || empty( $allow_batch['v1'] ) ) {
    				$error = new WP_Error(
    					'rest_batch_not_allowed',
    					__( 'The requested route does not support batch requests.' ),
    					array( 'status' => 400 )
    				);
    			}
    		}

    		if ( ! $error ) {
    			$check_required = $single_request->has_valid_params();
    			if ( is_wp_error( $check_required ) ) {
    				$error = $check_required;
    			}
    		}

    		if ( ! $error ) {
    			$check_sanitized = $single_request->sanitize_params();
    			if ( is_wp_error( $check_sanitized ) ) {
    				$error = $check_sanitized;
    			}
    		}

    		if ( $error ) {
    			$has_error    = true;
    			$validation[] = $error;
    		} else {
    			$validation[] = true;
    		}
    	}

    	$responses = array();

    	if ( $has_error && 'require-all-validate' === $batch_request['validation'] ) {
    		foreach ( $validation as $valid ) {
    			if ( is_wp_error( $valid ) ) {
    				$responses[] = $this->envelope_response( $this->error_to_response( $valid ), false )->get_data();
    			} else {
    				$responses[] = null;
    			}
    		}

    		return new WP_REST_Response(
    			array(
    				'failed'    => 'validation',
    				'responses' => $responses,
    			),
    			WP_Http::MULTI_STATUS
    		);
    	}

    	foreach ( $requests as $i => $single_request ) {
    		if ( is_wp_error( $single_request ) ) {
    			$result      = $this->error_to_response( $single_request );
    			$responses[] = $this->envelope_response( $result, false )->get_data();
    			continue;
    		}

    		$clean_request = clone $single_request;
    		$clean_request->set_url_params( array() );
    		$clean_request->set_attributes( array() );
    		$clean_request->set_default_params( array() );

    		/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
    		$result = apply_filters( 'rest_pre_dispatch', null, $this, $clean_request );

    		if ( empty( $result ) ) {
    			$match = $matches[ $i ];
    			$error = null;

    			if ( is_wp_error( $validation[ $i ] ) ) {
    				$error = $validation[ $i ];
    			}

    			if ( is_wp_error( $match ) ) {
    				$result = $this->error_to_response( $match );
    			} else {
    				list( $route, $handler ) = $match;

    				if ( ! $error && ! is_callable( $handler['callback'] ) ) {
    					$error = new WP_Error(
    						'rest_invalid_handler',
    						__( 'The handler for the route is invalid' ),
    						array( 'status' => 500 )
    					);
    				}

    				$result = $this->respond_to_request( $single_request, $route, $handler, $error );
    			}
    		}

    		/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
    		$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $single_request );

    		$responses[] = $this->envelope_response( $result, false )->get_data();
    	}

    	return new WP_REST_Response( array( 'responses' => $responses ), WP_Http::MULTI_STATUS );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/class-wp-rest-server.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api/class-wp-rest-server.php#L1741)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/class-wp-rest-server.php#L1741-L1900)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#hooks)󠁿

 [apply_filters( ‘rest_post_dispatch’, WP_HTTP_Response $result, WP_REST_Server $server, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_post_dispatch/)

Filters the REST API response.

 [apply_filters( ‘rest_pre_dispatch’, mixed $result, WP_REST_Server $server, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_pre_dispatch/)

Filters the pre-calculated result of a REST API dispatch request.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_Server::match_request_to_handler()](https://developer.wordpress.org/reference/classes/wp_rest_server/match_request_to_handler/)`wp-includes/rest-api/class-wp-rest-server.php` |

Matches a request object to its handler.

  | 
| [WP_REST_Server::respond_to_request()](https://developer.wordpress.org/reference/classes/wp_rest_server/respond_to_request/)`wp-includes/rest-api/class-wp-rest-server.php` |

Dispatches the request to the callback handler.

  | 
| [wp_parse_url()](https://developer.wordpress.org/reference/functions/wp_parse_url/)`wp-includes/http.php` |

A wrapper for PHP’s parse_url() function that handles consistency in the return values across PHP versions.

  | 
| [WP_REST_Request::__construct()](https://developer.wordpress.org/reference/classes/wp_rest_request/__construct/)`wp-includes/rest-api/class-wp-rest-request.php` |

Constructor.

  | 
| [WP_REST_Server::get_route_options()](https://developer.wordpress.org/reference/classes/wp_rest_server/get_route_options/)`wp-includes/rest-api/class-wp-rest-server.php` |

Retrieves specified options for a route.

  | 
| [WP_REST_Server::envelope_response()](https://developer.wordpress.org/reference/classes/wp_rest_server/envelope_response/)`wp-includes/rest-api/class-wp-rest-server.php` |

Wraps the response in an envelope.

  | 
| [WP_REST_Server::error_to_response()](https://developer.wordpress.org/reference/classes/wp_rest_server/error_to_response/)`wp-includes/rest-api/class-wp-rest-server.php` |

Converts an error to a response object.

  | 
| [wp_parse_str()](https://developer.wordpress.org/reference/functions/wp_parse_str/)`wp-includes/formatting.php` |

Parses a string into variables to be stored in an array.

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 8 more](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.6.0](https://developer.wordpress.org/reference/since/5.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_server%2Fserve_batch_request_v1%2F)
before being able to contribute a note or feedback.