Use a staging file until the response is known to be the expected object
AISwapLab content downloads require API authentication and ownership; a website session cookie is not a replacement for the API bearer credential. The content route can also deliver chat JSON, so application/json is not universally an error: compare it with the expected job kind and state. An HTTP success alone is insufficient to establish a complete usable image or clip. A partial 206 response may represent only one byte range, not a finished full download.
What to do next
- 01
Check readiness and use the owned content route
Read the job and wait for completed with a content_url. Resolve that documented route against the expected AISwapLab origin and authenticate from your server. Do not send the key to an arbitrary URL supplied by untrusted input or expose it in a public result link.
- 02
Validate before writing the final filename
Inspect HTTP status and Content-Type first. For a media job, handle an error body as an error even if the requested filename ends in a media extension. Write to a temporary local file while transferring, distinguish a complete 200 download from intentionally assembled byte ranges, and preserve a redacted diagnostic if the transfer fails.
- 03
Verify the actual artifact and promote it
Check nonempty file size and open or decode the completed artifact with an appropriate local media tool. For a text job, validate the expected JSON instead. Only after validation move the staged file to the final application result and allow creative review. Keep delivery errors attached to the original job.
A concrete example
Illustrative integration: a worker requests an image but receives a 401 JSON response after its key expires. A naive downloader writes those bytes as result.png. A response-aware worker records the authentication error, leaves no final image and restores the approved credential configuration. It then retrieves the existing completed job rather than submitting a new image. A separate chat task legitimately downloads JSON, so the validation rule uses the task's expected kind instead of banning JSON everywhere.
Download gate
| Check | Reject or hold when |
|---|---|
| Job state | Not completed or no content route |
| Response | Error status or unexpected type for job kind |
| Transfer | Incomplete body or unassembled partial range |
| Artifact | Cannot parse/decode as expected |
Before you call it ready
- Status, expected job kind and Content-Type are checked together.
- Partial or error bytes never become an approved final media file.
- The actual downloaded object is opened or parsed before delivery.
When to stop or choose another method
Stop final delivery when the response or artifact fails validation. Fix authentication, readiness or transfer handling on the same job; a new generation cannot repair a downloader that saves error pages as media.
