This is where I spent most of my time and still walked away unsure I got it right.
Start by explaining how you would extract the runtime ID from the first response and store it in a variable. Then describe a generic substitution function that recursively traverses request URLs and JSON bodies, replacing any hard-coded placeholder with the actual ID. Finally, discuss verification steps such as logging the transformed request and asserting the absence of the placeholder before dispatch.
Pro tip: Use a unique, unmistakable placeholder (e.g., `{{RUNTIME_ID}}`) instead of a real-looking ID to avoid accidental collisions and make verification trivial. Also, consider using a templating library like Handlebars or Mustache for safe substitution, but be cautious of injection risks if the ID comes from an untrusted source.
Parse the first API response to extract the generated ID and store it in a variable (e.g., `runtimeId`). Ensure you handle potential parsing errors and validate the ID format.
Create a function that takes a request object (URL and body) and replaces all occurrences of the hard-coded placeholder with the runtime ID. For nested JSON, use a recursive traversal to handle objects and arrays.
For each of the remaining API calls, pass the request through the substitution function before sending. Ensure the function is applied to both the URL and the body (including nested fields).
Before dispatching, log or assert that the placeholder no longer exists in the request and that the runtime ID is present. Optionally, compare the transformed request against a schema or expected pattern.
Consider using a templating engine with escaping to prevent injection, and handle cases where the placeholder might appear in unexpected places (e.g., headers). Also, ensure idempotency if the substitution is retried.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.