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

631
Visualizações
User Agent missing error in ReactJS fetch api

I am using News API in ReactJS for fetching news using fetch API.

CODE FRAGMENT

const url = `https://newsapi.org/v2/top-headlines?country=in&category=${this.props.category}&pageSize=${this.state.pageSize}&page=${this.state.page}`;
let data = await fetch(url);

But when I run it on the localhost, it gave an error 400 saying:

{status: 'error', code: 'userAgentMissing', message: 'Please set your User-Agent header to identify your application. Anonymous requests are not allowed.'}

When I searched the same on Stack Overflow, I got a link of a similar problem here:

User Agent Missing error when making a get request to an API

Then I added the User-Agent header in the fetch API's argument:

CODE FRAGMENT

headers = new Headers({
        "X-Api-Key": this.apiKey,
        "User-Agent": navigator.userAgent
    });
const url = `https://newsapi.org/v2/top-headlines?country=in&category=${this.props.category}&pageSize=${this.state.pageSize}&page=${this.state.page}`;
let data = await fetch(url, {
            headers: this.headers
        });

But still it shows the same error:

{status: 'error', code: 'userAgentMissing', message: 'Please set your User-Agent header to identify your application. Anonymous requests are not allowed.'}

This error is asking for "User-Agent" in the request header, but it is already there! When I looked into the network section of the chrome browser.

Screenshot of the network section of chrome

So, what's exactly the problem is? And how to solve this issue?

about 4 years ago · Juan Pablo Isaza
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