Javascript, write a script that fetches an API with a increasing index. URL:…
- User
Javascript, write a script that fetches an API with a increasing index. URL: https://example.com/example_api.php?id=10000 The script should fetch the API, increment index every time, only fetch once a second, and save it all to a string for ease of export. Include min-max ranges for ID.
Assistant
// Configuration const baseUrl = 'https://example.com/example_api.php'; const minId = 10000; // Starting ID const maxId = 10100; // Ending ID - adjust as needed const delayBetweenRequests = 1000; // 1 second in milliseconds // Initialize result string let allResponses = ''; // Function to fetch from API with specific ID async function fetchFromAPI(id) { try { const response = await fetch(`${baseUrl}?id=${id}`); const data = await response.text(); return data; } catch (error) { return `Error fetching id ${id}: ${error.message}`; } } // Main function to fetch all IDs in sequence async function fetchAllIds() { console.log(`Starting to fetch IDs from ${minId} to ${maxId}`); for (let id = minId; id <= maxId; id++) { console.log(`Fetching ID: ${id}`); // Fetch the current ID const responseData = await fetchFromAPI(id); // Append to collected responses with ID identifier allResponses += `\n--- ID: ${id} ---\n${responseData}\n`; // Wait 1 second before next request (if not the last one) if (id < maxId) { await new Promise(resolve => setTimeout(resolve, delayBetweenRequests)); } } console.log('All requests completed'); return allResponses; } // Start the process fetchAllIds();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.