In this lesson, you will learn how to parse the HTTP 207 Multi-Status response to granularly track the outcome of individual operations within a single batch request.
When working with APIs that support bulk operations—such as WebDAV or specific batch-processing endpoints—a standard 200 OK or 4xx/5xx error code is insufficient. If you submit a request that modifies five different resources, three might succeed, one might fail due to a validation error, and one might encounter a server timeout. The 207 Multi-Status code is used to convey this "partial success," where the overall response body contains a collection of individual status codes for each operation.
Parsing Multi-Status Responses
Usually, these responses are formatted in XML (or sometimes nested JSON) containing multiple <response> elements, each with its own status code. To handle this in Python, you must iterate through the response payload to isolate individual results.
Using the requests library and a standard XML parser like xml.etree.ElementTree, you can process these as follows:
import xml.etree.ElementTree as ET
import requests
response = requests.post("https://api.example.com/batch", json=data)
if response.status_code == 207:
root = ET.fromstring(response.content)
# Iterate through individual operation results
for entry in root.findall('.//{DAV:}response'):
href = entry.find('{DAV:}href').text
status = entry.find('{DAV:}status').text
if "200" in status or "201" in status:
print(f"Success for {href}")
else:
print(f"Failure for {href} with status: {status}")
The critical challenge here is not identifying that the request finished, but ensuring your application logic doesn't treat the entire request as a failure just because one component failed. By extracting the status from each sub-element, you can implement retry logic selectively for only the items that returned errors (like a 503 Service Unavailable), rather than re-submitting the entire batch and risking duplicate operations for the tasks that already succeeded.
Key Takeaway: A 207 Multi-Status response requires shifting your error handling logic from the HTTP layer to the payload level, where you must programmatically inspect individual status codes to distinguish successful operations from those requiring retry or intervention.