# Files API Replacing File Content (Node JS / Next JS)

**URL:** <https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098>\
**Category:** APIs & Integrations\
**Tags:** cms-content\
**Created:** [May 26, 2023, 12:49pm UTC](https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098 "2023-05-26T12:49:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SRitchot](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sritchot/32/82797_2.png) [@SRitchot](https://community.hubspot.com/u/SRitchot)\
**Post date:** [May 26, 2023, 12:49pm UTC](https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098/1 "2023-05-26T12:49:57Z")

</div>

Hi,  
I have been trying to send a replace file content request from my server (Node JS).  
I want to update the content of a json file, but I am getting a 400 error:  
Here’s my code:

const FormData = require(‘form-data’);

var hubspotHeaders = new Headers();

// Tried the different headers

// hubspotHeaders.set(‘content-type’, ‘multipart/form-data’);

// hubspotHeaders.set(

// ‘content-type’,

// ‘multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW’

// );

hubspotHeaders.set(‘content-type’, ‘application/x-www-form-urlencoded’);

hubspotHeaders.append(‘authorization’, `Bearer AccessKey`);

const formData = new FormData();

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

const requestOption: RequestInit = {

method: ‘PUT’,

headers: hubspotHeaders,

body: formData,

};

const response = await fetch(`${_hubspotFileApiBaseUrl}/${fileId}`, requestOption);

Regards,

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [May 28, 2023, 2:27pm UTC](https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098/2 "2023-05-28T14:27:12Z")

</div>

Hi @SRitchot

The problem is that you are trying to use the multipart/form-data content type for a PUT request. The multipart/form-data content type is only used for POST requests. For PUT requests, you should use the application/x-www-form-urlencoded content type.  
To fix this, change the following line:  
Code snippet  
hubspotHeaders.set(‘content-type’, ‘multipart/form-data’);

To this-  
Code snippet  
hubspotHeaders.set(‘content-type’, ‘application/x-www-form-urlencoded’);

Once you have made this change, you should no longer get a 400 error.  
Here is the complete code:  
Code snippet  
const FormData = require(‘form-data’);  
var hubspotHeaders = new Headers();

// Tried the different headers  
// hubspotHeaders.set(‘content-type’, ‘multipart/form-data’);  
// hubspotHeaders.set(  
// ‘content-type’,  
// ‘multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW’  
// );  
hubspotHeaders.set(‘content-type’, ‘application/x-www-form-urlencoded’);

const formData = new FormData();  
formData.append(‘file’, fs.createReadStream(filePath));  
const requestOption: RequestInit = {  
method: ‘PUT’,  
headers: hubspotHeaders,  
body: formData,  
};  
const response = await fetch(`${_hubspotFileApiBaseUrl}/${fileId}`, requestOption);

Hope this will helps you out. Please mark it as Solution Accepted to help other Community member.

Thanks!

---

<div class="post-metadata">

**Author:** ![SRitchot](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sritchot/32/82797_2.png) [@SRitchot](https://community.hubspot.com/u/SRitchot)\
**Post date:** [May 28, 2023, 5:30pm UTC](https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098/3 "2023-05-28T17:30:15Z")

</div>

Hi @GRajput ,

I have tried what you suggested and now I get a 415 error “Unsupported Media Type”.  
I am trying to replace the content of a JSON file with a new JSON file, is that a problem?

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [May 29, 2023, 12:05pm UTC](https://community.hubspot.com/t/files-api-replacing-file-content-node-js-next-js/82098/4 "2023-05-29T12:05:06Z")

</div>

Hi @SRitchot

Yes, it is a problem. The 415 error means that the server is not able to understand the format of the data that you are trying to send. In this case, the server is expecting a JSON file, but you are trying to send a new JSON file.  
To fix this error, you need to make sure that the data that you are sending is in the correct format. In this case, you need to make sure that the new JSON file is in the same format as the original JSON file.  
You can use a JSON parser to verify that the new JSON file is in the correct format. There are many different JSON parsers available, such as the JSONLint parser.  
Once you have verified that the new JSON file is in the correct format, you can try sending it to the server again. If the error still occurs, then you may need to contact the server administrator for help.  
**Here are some additional tips for fixing the 415 error:**  
Make sure that the Content-Type header is set to application/json.  
Make sure that the file extension of the JSON file is .json.  
Use a JSON parser to verify that the JSON file is valid.  
Contact the server administrator for help.

In futrue if you have any further query you can book a meeting. Please to do mark the reply as Solution Accepted and upvote.

Thanks!
