routa.media stores a file in Routa and retrieves the media object. For the concept, the accepted types and the limits, see Media.
media.upload() exists from version 0.3.0. The sample of a template header, used to create the template, does not go through here: use templates.uploadMedia(). The file each template send carries does.upload
There are two modes, and you use exactly one of them. The two form a type union: mixing them, or passing file without contentType, does not compile.
Upload the bytes
contentTypeis sent as theContent-Typeof the request, and that is how the API finds out the type. The SDK does not read it from thetypeof aBlob.- It must be exactly one of the accepted types, with no parameters such as
; charset=.... - The first bytes of the file must match the type. A PNG declared as
image/jpegis refused. - Do not pass
application/jsonascontentType: the API reads it as URL mode and respondsmedia_source_url_invalid.
Ask Routa to download a URL
http or https URL and stores the file. The SDK sends it as JSON, in { "source_url": ... }.
The result
The upload is synchronous: when it finishes, theMedia already has status: 'ready' and a signed url, valid for 15 minutes. retrieve() issues a new URL.
Keep the id. From 0.5.0, messages.send() accepts it as the media header of a template, in headerParameter.mediaId. No method sends a media message yet: see what the SDK does not support yet.
retrieve
Errors
The SDK does not check the size or the type of the file. The API decides, and a rule copied into the SDK would go stale. A refused upload throws:
None of these errors sets
param. The 413 arrives as a base RoutaApiError because the SDK has no subclass for it.
Not idempotent
POST /v1/media does not read Idempotency-Key, so the SDK does not send one. The client still retries a network failure or a 5xx. If the first attempt had already stored the file and only the response was lost, the retry stores a second copy, with another id.