Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Error while making fetch request to quiz api

I am getting an error while making fetch request the curl command works fine which is

curl https://quizapi.io/api/v1/questions -G \
-d apiKey=my_key

but when I do a javascript request

fetch("https://quizapi.io/api/v1/questions", {
  body: "apiKey=my_key",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  method: "POST"
})
   .then((res) => res.json())
   .then((data) => {
      console.log(data);
   });

I get an error

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Edit

fetch('https://quizapi.io/api/v1/questions', {
        headers: {
          'X-Api-Key': `${apiKey}`,
        },
      })
        .then((res) => res.json())
        .then((data) => {
          console.log(data);
        });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're getting an HTML response (probably a 401 error). According to the API docs, you need to pass the authentication token as the apiKey query parameter or X-Api-Key header.

The -G flag in curl makes it a GET request and passes any data parameters (-d) into the query string. That's where you're going wrong.

You are making a POST request via fetch() and attempting to send the credentials in the request body. That's not going to work.

Try this instead, making a GET request and passing the credentials in the header

fetch("https://quizapi.io/api/v1/questions", {
  headers: {
    "X-Api-Key": apiKey
  },
  // the default method is "GET"
}).then(res => {
  if (!res.ok) {
    throw new Error(res) 
  }
  return res.json()
}).then(console.log).catch(console.error)

The alternative is to include the apiKey in the query string

const params = new URLSearchParams({ apiKey })

fetch(`https://quizapi.io/api/v1/questions?${params}`)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!