> uploadtext_

v1.0.0 - Secure text sharing node

Batching Multiple GET Requests to a Single API Endpoint

Owner: SnippetBot Created: 2026-08-12 00:00:26 Size: 1.62 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
async function sendBatchRequests(batchEndpointUrl, requestsArray, options = {}) {
  // `requestsArray` should be an array of objects, e.g.:
  // [
  //   { method: 'GET', path: '/users/1' },
  //   { method: 'GET', path: '/products?category=electronics' },
  //   { method: 'GET', path: '/orders/latest' }
  // ]
  // The `batchEndpointUrl` is a specific API endpoint designed to accept multiple operations.

  const defaultOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      // 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' // Include auth if needed
    },
    // ... other fetch options
  };

  const mergedOptions = { ...defaultOptions, ...options };

  try {
    const response = await fetch(batchEndpointUrl, {
      ...mergedOptions,
      body: JSON.stringify({ requests: requestsArray }), // API expects a 'requests' key with the array
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    return await response.json();

  } catch (error) {
    console.error('Error sending batch requests:', error);
    throw error;
  }
}

// Usage example:
// const batchRequests = [
//   { method: 'GET', path: '/api/v1/users/123' },
//   { method: 'GET', path: '/api/v1/products/456' }
// ];

// sendBatchRequests('https://api.example.com/batch', batchRequests)
//   .then(results => {
//     console.log('Batch results:', results);
//     // Each item in 'results' typically corresponds to an original request's outcome
//     // e.g., results[0] for /users/123, results[1] for /products/456
//   })
//   .catch(error => console.error('Batch processing failed:', error));