Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda