Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API Fetch Request not working - CORS issue even with Authorization Bearer API Key #604

Open
adriaanbalt opened this issue Feb 6, 2025 · 0 comments

Comments

@adriaanbalt
Copy link

As per this webpage I am trying to use the REST API as a fetch request within my codebase, instead of a CURL.

I have setup this code:

const LLAMA_CLOUD_API_KEY = process.env.LLAMA_CLOUD_API_KEY
const LLAMA_CLOUD_BASE_URL = 'https://api.cloud.llamaindex.ai/api/parsing';

export default async function (file: File): Promise<void> {
  const formData = new FormData();
  formData.append('file', file);
  const uploadResponse = await fetch(`${LLAMA_CLOUD_BASE_URL}/upload`, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      Authorization: `Bearer ${LLAMA_CLOUD_API_KEY}`,
    },
    body: formData,
  });
  if (!uploadResponse.ok) {
    console.error('File upload failed:', await uploadResponse.text());
    return;
  }

  const { job_id } = await uploadResponse.json();
  console.log('File uploaded successfully, Job ID:', job_id);

  // Step 2: Poll the job status until parsing is complete
  let jobStatus: string;
  do {
    await new Promise(resolve => setTimeout(resolve, 2000)); // Poll every 2 seconds
    const statusResponse = await fetch(`${LLAMA_CLOUD_BASE_URL}/job/${job_id}`, {
      headers: {
        Authorization: `Bearer ${LLAMA_CLOUD_API_KEY}`,
        Accept: 'application/json',
      },
    });

    if (!statusResponse.ok) {
      console.error('Failed to fetch job status:', await statusResponse.text());
      return;
    }

    const statusData = await statusResponse.json();
    jobStatus = statusData.status;
    console.log('Job Status:', jobStatus);
  } while (jobStatus !== 'completed');

  console.log('Job completed! Fetching results...');

  // Step 3: Fetch the parsing results in Markdown format
  const resultResponse = await fetch(
    `${LLAMA_CLOUD_BASE_URL}/job/${job_id}/result/markdown`,
    {
      headers: {
        Authorization: `Bearer ${LLAMA_CLOUD_API_KEY}`,
        Accept: 'application/json',
      },
    },
  );

  if (!resultResponse.ok) {
    console.error('Failed to fetch job result:', await resultResponse.text());
    return;
  }

  const resultMarkdown = await resultResponse.text();
  console.log('Parsing Results in Markdown:\n', resultMarkdown);
}

But i always get a 400 Bad request "strict-origin-when-cross-origin".

I have tested my API Key locally with the CURL in the above website and it works. It just wont work within my JavaScript.

Any suggestions as to why this wouldn't work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant