POST /file error
# support
m
Hi, I'm trying to use the POST /file endpoint to upload files for the knowledge base from Coda. However, when I'm using the endpoint to send file data as a buffer, I get an error. Here's the
Node.js
code I'm trying to use (note that context.fetcher.fetch() is a Coda-specific fetch function):
Copy code
const form = new FormData();
    form.append("file", knowledgeBaseText);
    let response = await context.fetcher.fetch({
      url: "https://api.vapi.ai/file",
      method: "POST",
      headers: { ...form.getHeaders() },
      body: form.getBuffer(),
    });
However, when I use getBuffer, I get an error:
400 - {"message":"File is required","error":"Bad Request","statusCode":400}
Is it possible to have the POST /file endpoint accept the form buffer? I'm not able to send in the form as the body, and I'd like to have the buffer and the entire form work interchangeably. Thanks in advance for the help,
v
@mirandamon. Hello! It seems like you're on the right track with using
FormData
to construct your request body for file uploads. However, the error you're encountering indicates that the server is not receiving the file data as expected. When appending the file to the
FormData
, make sure that the second parameter is a
Buffer
or a
Stream
that represents the file data, and also includes a filename in the options. Here's an example of how you might adjust your code:
Copy code
javascript
const form = new FormData();
form.append("file", knowledgeBaseText, { filename: "knowledge-base.txt" });
let response = await context.fetcher.fetch({
  url: "https://api.vapi.ai/file/upload", // Make sure the URL is correct
  method: "POST",
  headers: {
    ...form.getHeaders(),
    'Authorization': 'Bearer YOUR_ACCESS_TOKEN', // Ensure you include the authorization header
  },
  body: form, // Use the form directly instead of getBuffer()
});
Please replace
'YOUR_ACCESS_TOKEN'
with your actual access token and ensure that the
knowledgeBaseText
variable contains the file data as a
Buffer
or
Stream
. Also, specify the correct filename in the options object. If you continue to face issues, please ensure that the endpoint URL is correct and that you have the necessary permissions to upload files. If the problem persists, please ask support for more info on this.
You can continue this conversation by mentioning me in the message.
You need to upload the file in multipart/form-data format
m
headers: { ...form.getHeaders() },
actually gives the multipart/form-data header
but i found out that adding the
{ filename: "knowledge-base.txt" }
in the form.append() fixed the issue
hmm, but now I'm running into a different error
Getting a 403 from Cloudflare - running this from my CLI works, but running it from my Coda integration fails
s
Please change your ip address.
m
hmm, I did, but it still doesn't work. it happens when calling this through a Coda pack
s
Did you tried using vpn?
5 Views