This question evaluates proficiency in runtime ID propagation across HTTP requests, JSON parsing and serialization, nested data traversal, and type-preserving replacement in request URLs and bodies; it is categorized under Coding & Algorithms and focuses on practical application rather than purely conceptual understanding.
You are given a sequence of 5 API requests that must be executed in order.
POST
) succeeds and returns
200 OK
with a JSON response containing a newly created resource ID.
"123"
) in their
URL path and/or JSON body
instead of the ID returned by Request 1.
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:
old_id
in the request
URL
is replaced with the runtime ID from
first_response_body
.
old_id
anywhere inside the request
body
(including nested fields, arrays, and string values) is replaced with the runtime ID.
null
, a JSON object, or a JSON string (in which case you should parse it, modify it, then serialize it back).
Return the updated list of request objects.