Request failed with status code 415 on File manager API v3

Hey all!

I’ve been trying to uplaod a file to file

manager

using this

endpoint

POST /filemanager/api/v3/files/upload

But idk what’s wrong with my script, as I’m not reading from any file as given in example. I’m just getting the file in binary form from an Url and after buffer making it a readable stream object and passing it to the above mentioned end point

Here’s my script:

async function upload() {

    const file\_path = url

    const response = await axios.get(file\_path,{responseType: 'arraybuffer'});

    var postUrl = 'https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=key';

if(response.status ==200){

    const buf = new Buffer.from(response.data, "base64");

    const bs = new stream.PassThrough();

    console.log(bs)

    var fileOptions = {

        access: 'PUBLIC\_INDEXABLE',

        ttl: 'P3M',

        overwrite: false,

        duplicateValidationStrategy: 'NONE',

        duplicateValidationScope: 'ENTIRE\_PORTAL'

    };

    var formData = {

        file: bs,

        options: JSON.stringify(fileOptions),

        folderPath: 'docs'

    };

    axios.post(postUrl, formData).then(function(response){

        console.log("Test")

        console.log(response)

    })

};

return false

}

Hi @UmairKhan ,
Hi , although your code looks fine to me , but could you please share the response what you are getting after hitting the code. also can you please try this code for uploading the file var request = require(‘request’);
var fs = require(‘fs’);
var postUrl = ‘https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo’;
var filename = ‘example_file.txt’;
var fileOptions = {
access: ‘PUBLIC_INDEXABLE’,
ttl: ‘P3M’,
overwrite: false,
duplicateValidationStrategy: ‘NONE’,
duplicateValidationScope: ‘ENTIRE_PORTAL’
};
var formData = {
file: fs.createReadStream(filename),
options: JSON.stringify(fileOptions),
folderPath: ‘docs’
};
request.post({
url: postUrl,
formData: formData
}, function optionalCallback(err, httpResponse, body){
return console.log(err, httpResponse.statusCode, body);
}); Refrence doc link :https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file the output should look like this https://legacydocs.hubspot.com/docs/methods/files/get_files_file_id

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regards.

Thanks @webdew for responding

My code is working fine if I use file in filename and use readStream to read the file. But whenever I get the attachment from the external Url and using buffer or PassThrough stream, I send it to hubspot server I get this error

UnhandledPromiseRejectionWarning: Error: Request failed with status code 415

@UmairKhan ,

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request’s indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regards.

@webdew I know what 415 means, that’s why I asked question here in first place. I just wanna know the exact problem in my payload which I’m sending to Hubspot file API and what kind of data hubspot is receiving from my end which is causing the actual problem.

I was trying to download file from the url and then upload the binary data of that file to the Hubspot Api endpoint. But for the mentioned endpoint you can’t directly upload that binary data, there must be a file in your system in which your data exists, then you directly upload that file from your system. For uploading file from the url, you have different endpoint which is “/files/v3/files/import-from-url/async”