WordPress REST / safe media upload

Upload first. Patch metadata second.

Use a code snapshot when the article explains a technical pattern but the body still needs copyable code.

upload-media.py

media = requests.post(
    f"{BASE}/media",
    auth=AUTH,
    headers=headers,
    data=png_bytes,
).json()

requests.post(
    f"{BASE}/media/{media['id']}",
    auth=AUTH,
    json={
        "title": title,
        "alt_text": alt_text,
        "caption": caption,
    },
)

Why this matters

The upload endpoint stores the file, but SEO metadata often needs a second request.

Gotcha

Never build Gutenberg payloads with shell heredocs when block comments must survive intact.