<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error 415 when uploading files in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772730#M62501</link>
    <description>&lt;P&gt;Have you tried setting the content type as&amp;nbsp;multipart/form-data?&lt;/P&gt;</description>
    <pubDate>Wed, 22 Mar 2023 23:55:59 GMT</pubDate>
    <dc:creator>JairoPy</dc:creator>
    <dc:date>2023-03-22T23:55:59Z</dc:date>
    <item>
      <title>Error 415 when uploading files</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772682#M62499</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When trying to upload a PDF file via fetch(), I keep getting a 415 error. The PDF file is saved in the same directory as the js file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see my function. I have tried changing the "Content-Type" in headers to several different options, and all spit back 400 and 415 errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I use request.post() instead of fetch(), I can upload the file. But I want to use fetch(). So what's the issue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help you can provide would be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;async function uploadFile(filePath, extension, timestamp) {

    const url = "https://api.hubapi.com/files/v3/files";
    var filename = `${filePath}.${extension}`;

    var fileOptions = {
        access: 'PRIVATE',
        overwrite: false,
        duplicateValidationStrategy: 'NONE',
        duplicateValidationScope: 'ENTIRE_PORTAL'
    };

    var formData = {
        file: fs.createReadStream(filename),
        fileName: `${filename} (${timestamp}).${extension}`,
        options: JSON.stringify(fileOptions),
        folderPath: 'Quotations'
    };

    try {
        const response = await fetch(url, { 
            "method": "POST",
            "formData": formData,
            "headers": {
                'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
                "Content-Type": "application/pdf"
            }
        });

        if(!response.ok) {
            throw new Error(`HTTP Error: ${response.status}`);
        }
        
        const data = await response.json();
        return data;

    } catch(error) {
        console.log(`Error: ${error}`);
    }

}

const fileId = uploadFile("Quotation", "pdf", getCurrentTimestamp());
fileId.then((data) =&amp;gt; console.log(data));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 21:54:58 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772682#M62499</guid>
      <dc:creator>TaylorAran</dc:creator>
      <dc:date>2023-03-22T21:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Error 415 when uploading files</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772730#M62501</link>
      <description>&lt;P&gt;Have you tried setting the content type as&amp;nbsp;multipart/form-data?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 23:55:59 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772730#M62501</guid>
      <dc:creator>JairoPy</dc:creator>
      <dc:date>2023-03-22T23:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Error 415 when uploading files</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772829#M62506</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/529092"&gt;@JairoPy&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I have tried that, but the reponse is always: "Error: Error: HTTP Error: 400".&lt;/P&gt;&lt;P&gt;I have also tried "application/json", which returns: "Error: Error: HTTP Error: 415".&amp;nbsp;&lt;/P&gt;&lt;P&gt;Commenting out the "Content-Type" line returns:&amp;nbsp;"Error: Error: HTTP Error: 400".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the sake of trying everything, I also tried "text/plain", but that returns&amp;nbsp;"Error: Error: HTTP Error: 415".&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 07:49:28 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772829#M62506</guid>
      <dc:creator>TaylorAran</dc:creator>
      <dc:date>2023-03-23T07:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error 415 when uploading files</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772982#M62522</link>
      <description>&lt;P&gt;Well, I guess the issue is that the fetch api as it is client side is subject to cross origin problems, as this endpoint requests authorization you just be able to reach it from your backend, that's why you're request works with the request node package but no with fetch api.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want to fetch any info from this api to the frontend, you could create an internal api to gather the data from hubspot and expose it to your client side.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 12:56:06 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Error-415-when-uploading-files/m-p/772982#M62522</guid>
      <dc:creator>JairoPy</dc:creator>
      <dc:date>2023-03-23T12:56:06Z</dc:date>
    </item>
  </channel>
</rss>

