Debug chained API requests by propagating a runtime ID
You are given a sequence of 5 API requests that must be executed in order.
-
Request 1 (a
POST
) succeeds and returns
200 OK
with a JSON response containing a newly created resource ID.
-
Requests 2–5 currently fail because they incorrectly use a
hardcoded
ID (e.g.,
"123"
) in their
URL path and/or JSON body
instead of the ID returned by Request 1.
Task
Write a function that, given:
-
requests
: a list of request objects in execution order. Each request has:
-
method
(string)
-
url
(string)
-
headers
(map of string → string)
-
body
(either
null
, a JSON object, or a JSON string)
-
first_response_body
: the parsed JSON body returned by Request 1, which includes the runtime ID (for example under
id
)
-
old_id
: the hardcoded ID value currently present in later requests
returns a new list of requests where, for Requests 2–5:
-
Every occurrence of
old_id
in the request
URL
is replaced with the runtime ID from
first_response_body
.
-
Every occurrence of
old_id
anywhere inside the request
body
(including nested fields, arrays, and string values) is replaced with the runtime ID.
Requirements / Notes
-
Do
not
modify Request 1.
-
The runtime ID may be numeric or string; preserve the correct type when updating JSON bodies.
-
The request body may be
null
, a JSON object, or a JSON string (in which case you should parse it, modify it, then serialize it back).
-
Assume all requests are well-formed; focus on correct propagation and replacement.
Output
Return the updated list of request objects.