Skip to main content

I’ve been trying calling the upload API for box in node.js application and keep getting 415 Unsupported_Media_Type. I gave the correct parent id. Also tried using both parent_id and parent: { ‘id’: ‘12345’}


response.status = 415


response.statusText = Unsupported Media Type



I also tried "Try this API’ on the developers page and just get ‘An unknown error occured’. When tried to get more details from the browser developer tools then I see that the call just returned as above 415 and unsupported media type message.



If I include 'Content-type" header with multipart/form-data then it doesn’t find the file part.



What is the wrong with the following code?



PS: I do not want to use SDK.



var parent = ‘12345’;


var token = 'Bearer ’ + accessToken;



const url = 'https://upload.box.com/api/2.0/files/content';

const fileData = fs.createReadStream("c:\\Work\\box-development\\CustomerRequest.txt");

let formData = new FormData();

formData.append('attributes', "{"name":"newfile.txt", "parent_id":"12345"}");

formData.append('file', fileData);



try{

let response = await fetch(url, {

method: 'POST',

body: formData,

headers: {

'Authorization': token

}

});



let responseData = await response.text();

console.log(response.status);

console.log(response.statusText);

console.log(responseData);



} catch (err) {

}

Hello 👋,



May I ask why you don’t want to use the SDK? It would manage authentication and the file upload process for you.



Thanks,


Alex, Box Developer Advocate 🥑


Because the node SDK is still using the deprecated request module and it has a vulnerability now. The github there is already an issue which is 3 years old and still not being fixed.



The best way I see now is to see in using API route. I got the authentication part taken care of - just the error messages doesn’t help much now 😦


I see~ I will need to test using Node without the SDK to upload something. Let me get back to you.


I’m not sure if you’ve already tried this… but our Postman collection might help here. You can open any of the endpoints. Then, on the right side, click the code button and select a language. It will write out the appropriate code for you.




Yes, I tried that route just in case if the code above mentioned is wrong. When I took the Postman generated code then also I get the same error. I also tried to send a string instead of actual file stream then I get 400 Bad request.



I even went the route of debugging the box-node-sdk to see what it does behind the scene and use the code without any success. Something is not right, for sure. But what it is 😦


Hmm. I tried the below code and it worked fine…



Replace FULL_PATH and REDACTED with your file path and token respectively. the file here is in a node project and I npm i’d axios and form-data.



const axios = require('axios');

const FormData = require('form-data');

const fs = require('fs');

let data = new FormData();

data.append('attributes', '{\n "name": "test.pdf",\n "parent": {\n "id": "0"\n },\n "content_created_at": "2012-12-12T10:53:43-08:00",\n "content_modified_at": "2012-12-12T10:53:43-08:00"\n}');

data.append('file', fs.createReadStream('FULL_PATH'));



let config = {

method: 'post',

maxBodyLength: Infinity,

url: 'https://upload.box.com/api/2.0/files/content',

headers: {

'Content-Type': 'multipart/form-data',

'Authorization': 'Bearer REDACTED'

...data.getHeaders()

},

data : data

};



axios.request(config)

.then((response) => {

console.log(JSON.stringify(response.data));

})

.catch((error) => {

console.log(error);

});


Reply