Skip to main content
Question

uploading file from js fetch api to box and keep getting 415 error. Can anyone please let me know what mistake I am making here? Thanks

  • May 22, 2025
  • 4 replies
  • 32 views

Forum|alt.badge.img
BOX1_Jhp6YvXft44Y_vzD7KqaoA.png  var header = boundary +
        "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + this.fileData[0].Title + " \"" +
        "\nContent-Type: application/octet-stream" ;
        var footer = boundary;

        const body = '{"name":' + this.fileData[0].VersionData + ', "parent":{"id": '+ 12345 + '}}';
       
        console.log('body--',body);
   
        fetch(uploadEndPointURL,{
            method: 'POST',
             mode: 'cors',
            cache: 'no-cache',
            Body: body,
           
        headers: {
         
           'Accept': '*/*',
            'Authorization': "Bearer "+this.accessToken,
            'Content-Type': 'application/json; charset=utf-8',
            'Access-Control-Allow-Origin': uploadEndPointURL,
            'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Credentials': true

        }
        }).then(function(response){
            console.log('response--',response);
            console.log(response.body);
           
        }).catch(function(error) {
            console.log('error--',error.response.data.message);
          })
        });

4 replies

Forum|alt.badge.img

Hi 

I think you might get this as you add the application/json header which should not be used in this call. try removing it and test again

Rgds,
Peter Christensen, Platform Solutions Engineer, Box


Forum|alt.badge.img

Hi 363373027287,

Thanks for the quick response.

I tried the above solution which you suggested but it didn't work. Still facing the same issue.

I want to upload files to box from js fetch api. One thing I need to confirm is in my app I also need to add the allowed origin to the value from where I am generating request right? I have added the origin value but the app is still not authorized by the box admin. So wanted to know is this the reason why I am facing this 415 issue. Right now, I am using some extension for by passing CORS only for testing.

BOX1_BozaZzK59b7YBXBJoTWt6A.png

Thanks


Forum|alt.badge.img

a 415 is normally when you send the wrong content-type header or no content-header with your request. A CORS issue would show as a 403 with a context saying something around CORS


Forum|alt.badge.img

@Peter Christensen, I have added the origin from where I am passing request to box i.e "https://contentintelligence.lightning.force.com" in the Allowed Origins but still I am getting "Access to XMLHttpRequest at 'https://upload.box.com/api/2.0/files/content' from origin 'https://contentintelligence.lightning.force.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."