Uploading Media

Media files are uploaded using HTTP PUT. You must set the following HTTP headers in the upload request:
  • " Content-type" =Β "<File content type>"
  • "X-Token" =Β "<Token>"

Bot Server upload URL format

https://<nandboxBotServer>:<port>/nandbox/upload/<imageName>

In the body of the request, include the binary content of the file. For more details, check the code examplesΒ here
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://<nandboxBotServer>:<port>/nandbox/upload/<imageName>'; // 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);
python
def 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://<nandboxBotServer>:<port>/nandbox/upload/<imageName>' # 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>", }