I’m calling the Box API directly from JavaScript to upload a new file. As the docs suggest, I run a preflight check first, and that check succeeds (200). The code for the preflight check looks like this:
const preflightResponse = await fetch(url, {
method: "OPTIONS",
headers: {
Authorization: `Bearer ${this.#token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name,
parent: {
id: parentId,
},
}),
});
After that step succeeds, I move on to the next step of uploading the actual file. For this text, I’m just sending the text “Hello, world!” to a file named hello.txt
. The parentId
is the folder to upload to, and it exists (otherwise the preflight check would fail). The code to upload the file looks like this:
const form = new FormData();
form.append("attributes", JSON.stringify({
name,
parent: {
id: parentId,
},
}));
form.append("file", new Blob(e"Hello, world!"]));
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${this.#token}`
},
body: form,
});
For this call, I get a 404 error. The body of the response is blank, so there’s no additional information given about a possible problem.
Some other notes about this:
- The
url
ishttps://api.box.com/2.0/files/content
. - I am using a developer token and testing on the account that is tied to that developer token.
- I was able to successfully create a folder using the API, and that’s the folder I’m trying to upload this file to.
- If you’re unfamiliar with JavaScript, passing
body: form
automatically sets the correctContent-type
header. - Other API calls also succeed without any problem using the same developer token.
At this point I’m out of ideas of what I could possibly be doing wrong, so any insights are gratefully appreciated.