httpresponse Unsupported Media Type API upload file

Hello,

i do use upload file API V3 in a Wix website, I start getting the error of unsupported Media Type, I don’t know, this is my code:

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

export async function post_image () { 
 const manageUrl = 'https://api.hubapi.com/filemanager/api/v3/files/upload';

 const fileOptions = { 
access: 'PUBLIC_INDEXABLE', 
overwrite: false,
ttl:"P3M", 
duplicateValidationStrategy: 'NONE', 
duplicateValidationScope: 'ENTIRE_PORTAL', 
}; 
const fileParams = { 
options: JSON.stringify(fileOptions), 
folderPath: 'docs', 
}; 
let filename = "ninja.jpg"; 
const formdata = new FormData();
 formdata.append('file',fs.createReadStream("https://static.wixstatic.com/media/d8ba6f_c155297693b242a0bd84d149dec28a82~mv2.jpg"),filename); 
 Object.keys(fileParams).forEach((key) => formdata.append(key, fileParams[key])); 
let otp = { 
method: 'POST', 
headers:{
 'Authorization': `Bearer ${'My_Token'}`,
 },
body:JSON.stringify(formdata), 
}; 
console.log('opt', otp); 
return await fetch(manageUrl, otp) 
.then((httpResponse)=>{
 if(httpResponse.ok){ 
return httpResponse.json(); 
}else{ 
console.log('httpresponse', httpResponse.statusText); 
return Promise.reject("Fetch did not succeed");
 }
 });}
 ---------------------------------------------------------------------------------------------

this is the console.log of opt:

{ “method”: “POST”,

“headers”: { “Authorization”: “Bearer MY_TOKEN” },

“body”: “{\”_overheadLength\“:366,\”_valueLength\“:143,\”_valuesToMeasure\“:[{\”_readableState\“:{\“objectMode\”:false,\“highWaterMark\”:65536,\“buffer\”:{\“head\”:null,\“tail\”:null,\“length\”:0},\“length\”:0,\“pipes\”:[],\“flowing\”:false,\“ended\”:false,\“endEmitted\”:false,\“reading\”:false,\“sync\”:true,\“needReadable\”:false,\“emittedReadable\”:false,\“readableListening\”:false,\“resumeScheduled\”:false,\“errorEmitted\”:false,\“emitClose\”:true,\“autoDestroy\”:false,\“destroyed\”:false,\“errored\”:null,\“closed\”:false,\“closeEmitted\”:false,\“defaultEncoding\”:\“utf8\”,\“awaitDrainWriters\”:null,\“multiAwaitDrain\”:false,\“readingMore\”:false,\“decoder\”:null,\“encoding\”:null},\”_events\“:{},\”_eventsCount\“:3,\“path\”:\”[https://static.wixstatic.com/media/d8ba6f\_c155297693b242a0bd84d149dec28a82~mv2.jpg\\](https://static.wixstatic.com/media/d8ba6f_c155297693b242a0bd84d149dec28a82~mv2.jpg\)

“,\“fd\”:null,\“flags\”:\“r\”,\“mode\”:438,\“end\”:null,\“autoClose\”:true,\“bytesRead\”:0,\“closed\”:false}],\“writable\”:false,\“readable\”:true,\“dataSize\”:0,\“maxDataSize\”:2097152,\“pauseStreams\”:true,\”_released\“:false,\”_streams\“:[\”----------------------------878248842902466855888581\\r\\nContent-Disposition: form-data; name=\\\“file\\\”; filename=\\\“ninja.jpg\\\”\\r\\nContent-Type: image/jpeg\\r\\n\\r\\n\“,{\“source\”:{\”_readableState\“:{\“objectMode\”:false,\“highWaterMark\”:65536,\“buffer\”:{\“head\”:null,\“tail\”:null,\“length\”:0},\“length\”:0,\“pipes\”:[],\“flowing\”:false,\“ended\”:false,\“endEmitted\”:false,\“reading\”:false,\“sync\”:true,\“needReadable\”:false,\“emittedReadable\”:false,\“readableListening\”:false,\“resumeScheduled\”:false,\“errorEmitted\”:false,\“emitClose\”:true,\“autoDestroy\”:false,\“destroyed\”:false,\“errored\”:null,\“closed\”:false,\“closeEmitted\”:false,\“defaultEncoding\”:\“utf8\”,\“awaitDrainWriters\”:null,\“multiAwaitDrain\”:false,\“readingMore\”:false,\“decoder\”:null,\“encoding\”:null},\”_events\“:{},\”_eventsCount\“:3,\“path\”:\” https://static.wixstatic.com/media/d8ba6f\_c155297693b242a0bd84d149dec28a82~mv2.jpg\\“,\\“fd\\”:null,\\“flags\\”:\\“r\\”,\\“mode\\”:438,\\“end\\”:null,\\“autoClose\\”:true,\\“bytesRead\\”:0,\\“closed\\”:false},\\“dataSize\\”:0,\\“maxDataSize\\”:null,\\“pauseStream\\”:true,\\”\_maxDataSizeExceeded\\“:false,\\”\_released\\“:false,\\”\_bufferedEvents\\“:\[{\\“0\\”:\\“pause\\”}],\\”\_events\\“:{},\\”\_eventsCount\\“:1},null,\\”\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-878248842902466855888581\\\r\\\nContent-Disposition: form-data; name=\\\“options\\\”\\r\\n\\r\\n\“,\”{\\\“access\\\”:\\\“PUBLIC_INDEXABLE\\\”,\\\“overwrite\\\”:false,\\\“ttl\\\”:\\\“P3M\\\”,\\\“duplicateValidationStrategy\\\”:\\\“NONE\\\”,\\\“duplicateValidationScope\\\”:\\\“ENTIRE_PORTAL\\\”}\“,null,\”----------------------------878248842902466855888581\\r\\nContent-Disposition: form-data; name=\\\“folderPath\\\”\\r\\n\\r\\n\“,\“docs\”,null],\”_currentStream\“:null,\”_insideLoop\“:false,\”_pendingNext\“:false,\”_boundary\“:\”--------------------------878248842902466855888581\“}” }

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

I don’t mention the Content-Type because I’m using FormData.

Can anyone see the issue here?

Thanks!

Hi @Arbi ,

When I try to visit the URL in the response, I get a forbidden message. This could be the cause of your issue.

Hey @Teun,

Thanks for your reply,

Yep when i click on the URL displayed in the console.log() it says that is “Forbidden” because it add a slash at the end of the static URL. This is weird because when you look at the formData the file URL doesn’t have “/” at the end.

Hi @Arbi ,

Does your code work if you try this image instead?

Hi @Teun,

I’ve tried with this image but it’s the same Error, “Unsupported Media Type”

Hi @Arbi ,

I found the solution. You are using the wrong endpoint. This endpoint expects a file to be uploaded that is currently available in your system, however, you want to download an image from an URL and upload that image. If that’s the case, you need this endpoint: /files/v3/files/import-from-url/async

Hi @Teun,

I’ve tried with the new endpoint and still getting the same error. I’ve noticed a new error “Error: Fetch did not succeed at c (https://static.parastorage.com/services/wix-code-viewer-app/1.1479.539/app.js:3:10527)”, which I don’t know it means. I think I can’t access Wix static files, that’s why I’m getting an “Unsupported Media Type” error.