Upload File returns 415 'Unsupported Media Type'

Hello,

Trying to POST a file (in form pdf) to Files API using the Node SDK, but getting back the following error:
" [Symbol(Response internals)]: {
url: ‘https://api.hubapi.com/filemanager/api/v3/files/upload’,
status: 415,
statusText: ‘Unsupported Media Type’,
}"
CODE:
--------------------------------------------------------------

async function uploadFile(filePath) {

console.log("calling uploadFile() for file: ", filePath);

const formData = new FormData();

formData.append(“folderPath”, ‘/’);

var options = {

access: ‘PUBLIC_INDEXABLE’,

ttl: ‘P3M’,

overwrite: true,

duplicateValidationStrategy: ‘NONE’,

duplicateValidationScope: ‘ENTIRE_PORTAL’

};

formData.append(“options”, JSON.stringify(options));

formData.append(“file”, fs.createReadStream(filePath));

const response = await hubspotClient.apiRequest({

method: ‘POST’,

path: ‘/filemanager/api/v3/files/upload’,

body: formData,

defaultJson: true,

headers: {

“Content-Type”: “application/json”

}

});

console.log(response);

}

--------------------------------------------------------------

I have seen many other threads in this community around this error, but they all just suggest setting the Content-Type header, but I am already doing that.
Any help would be greatly appreciated.

Thanks!

Hello @cwalker2

It seems that the server is rejecting the file you are trying to upload because the media type (MIME type) of the file is not supported by the API.

You mentioned that you are uploading a PDF file, so you should set the Content-Type header to “application/pdf” instead of “application/json”, which is the current value in your code. The “Content-Type”: “application/json” header is used to indicate that the request body is in JSON format, but in your case, you are sending a multipart/form-data request with a PDF file.

Try updating your code to set the correct Content-Type header and see if it resolves the issue

const response = await hubspotClient.apiRequest({
 method: 'POST',
 path: '/filemanager/api/v3/files/upload',
 body: formData,
 defaultJson: true,
 headers: {
 "Content-Type": "multipart/form-data"
 }
});

Hello @himanshurauthan, I really appreciate the quick reply/suggestion. I just tried with content-type header set, but instead of a 415 I get a ‘400 bad request’.

```

const response = await hubspotClient.apiRequest({

method: ‘POST’,

path: ‘/filemanager/api/v3/files/upload’,

body: formData,

defaultJson: true,

headers: {

“Content-Type”: “multipart/form-data”

}

});

```

Response:

------------

{
url: ‘https://api.hubapi.com/filemanager/api/v3/files/upload’,
status: 400,
statusText: ‘Bad Request’,
headers: Headers { [Symbol(map)]: [Object: null prototype] },
}

Anything else you think I could try? Many thanks

@himanshurauthan was able to get this working by doing a straight REST call with Axios instead of using the node sdk wrapper:

async function uploadFileAxios(filePath) {

let data = new FormData();

data.append(‘file’, fs.createReadStream(filePath));

data.append(‘folderPath’, ‘nda’);

data.append(‘options’, ‘{“access”: “PRIVATE”,“ttl”: “P3M”,“overwrite”: true,“duplicateValidationStrategy”: “NONE”,“duplicateValidationScope”: “ENTIRE_PORTAL”}’);

let config = {

method: ‘post’,

maxBodyLength: Infinity,

url: ‘https://api.hubapi.com/api/filemanager/api/v3/files/upload’,

headers: {

‘Authorization’: `Bearer ${HUBSPOT_ACCESS_TOKEN}`,

…data.getHeaders()

},

data : data

};

axios.request(config)

.then((response) => {

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

})

.catch((error) => {

console.log(error);

});

}

Did you ever get this sorted ? we are running into the exact same issue here and the API documentation doesn’t have any samples to follow