Uploading Media Files
Media files are uploaded using HTTPS PUT. The following HTTPS headers must be set in the upload request:
Content-Type
="<File content type>"
X-Token
="<Token>"
Media Server Upload URL Format
Use the following URL format to upload media files:
plain texthttps://<nandboxMediaServer>:<port>/nandbox/upload/<FileName>
Example:
plain texthttps://w1.nandbox.net:8080/nandbox/upload/<FileName>
Request Body
The body of the request must contain the binary content of the file being uploaded.
đ Notes:
- Ensure the file's MIME type is correctly set in the
Content-Type
header.
- The
X-Token
must be a valid authentication token for the request to succeed.
Supported Media Type
This table defines how content types (MIME types) are categorized based on the type of media being uploaded.
Media Type | Content Type (MIME Type) |
Text File | text/plain |
Image | image/jpeg , image/png , multipart/form-data |
GIF Image | image/gif |
Audio | audio/midi , audio/mp4 , audio/mpeg , audio/ogg , audio/basic |
Video | video/mp4 , video/mpeg , video/ogg |
File (Documents & Archives) | application/zip , application/pdf , application/msword , application/vnd.openxmlformats-officedocument.wordprocessingml.document , application/x-tar , application/x-7z-compressed , application/x-zip-compressed , application/x-rar-compressed , application/vnd.android.package-archive , application/vnd.ms-excel |
đ Notes:
- Text files, images, videos, audio files, and documents/archives are classified into different media types based on their MIME type.
- Multipart/form-data is considered an image upload.
javascript/** * Function to upload media using HTTP PUT request * * @param {File} file - The file to be uploaded * @param {string} token - The authentication token */ async function uploadMedia(file, token) { // Define the URL for the upload endpoint const url = 'https://<nandboxMediaServer>:<port>/nandbox/upload/<FileName>'; // Set the query parameters const params = new URLSearchParams({ p: 1, // Public upload (return link for the media to be downloaded) cache: 1 // Return size, width, height, and thumbnail only when p is 1 }); // Set the headers for the request const headers = new Headers({ 'Content-Type': file.type, // Set content type based on the file 'X-Token': token // Set the authentication token }); try { // Make the HTTP PUT request const response = await fetch(`${url}?${params.toString()}`, { method: 'PUT', headers: headers, body: file }); // Handle the response if (response.ok) { const result = await response.text(); // Adjust based on the expected response type console.log('Upload successful:', result); } else { console.error('Upload failed:', response.status, response.statusText); } } catch (error) { console.error('Error during upload:', error); } } // Usage example const file = new File(['file content'], 'filename.jpg', { type: 'image/jpeg' }); // Replace with actual file object const token = 'your_token_here'; // Replace with your actual token uploadMedia(file, token);
pythondef upload_media(file_path, token): """ Function to upload media using HTTP PUT request @param file_path: The path to the file to be uploaded @param token: The authentication token """ # Define the URL for the upload endpoint url = 'https://<nandboxMediaServer>:<port>/nandbox/upload/<FileName>' # Set the query parameters params = { 'p': 1, # Public upload (return link for the media to be downloaded) 'cache': 1 # Return size, width, height, and thumbnail only when p is 1 } # Read the file content with open(file_path, 'rb') as file: # Set the headers for the request headers = { 'Content-Type': 'image/jpeg', # Set content type based on the file 'X-Token': token # Set the authentication token } try: # Make the HTTP PUT request response = requests.put(url, params=params, headers=headers, data=file) # Handle the response if response.status_code == 200: result = response.text # Adjust based on the expected response type print('Upload successful:', result) else: print('Upload failed:', response.status_code, response.reason) except requests.RequestException as error: print('Error during upload:', error) # Usage example file_path = 'path/to/your/filename.jpg' # Replace with the actual file path token = 'your_token_here' # Replace with your actual token upload_media(file_path, token)
Response
đĸ 200
json{ "file" : "media_id", }