CaptureKit

Check the response

Make sure the answer is an image before you save it.

The Content-Type header gives the format of the file: image/png here (application/pdf with format=pdf). An error comes as JSON with an HTTP error status.

This code continues the script from Request a screenshot.

content_type = response.headers.get("content-type", "")
if not content_type.startswith("image/"):
    raise SystemExit(f"Expected an image, got {content_type}: {response.text}")
print(f"Content type: {content_type}")