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

377
Visualizações
blocked by CORS, No 'Access-Control-Allow-Origin' header - not working in react/axios but works fine in python/requests

The following python request is grabbing data from a paid API using private API key, and the fetch is working just fine:

import requests

api_key = 'our_private_api_key'
headers = { 'x-api-key': api_key }
params = {}
endpoint = 'our_api_endpoint_url'
resp: dict = requests.get(endpoint, headers=_headers_builder(headers), params=params).json().get('response', {})
resp_data = resp.get('data', None)

When fetching locally in Javascript (React App) using axios, we attempt with similar code, but in JavaScript (with the help of react-query's useQuery):

import { useQuery } from 'react-query';
import axios from 'axios'

async function fetchLiveData() {
    const api_key = 'our_private_api_key';
    const endpoint = 'our_api_endpoint_url';
    const { data } = await axios.get(endpoint, { headers: { 'x-api-key': api_key } });
    return data;
}

const { data, error, isError, isLoading: isLoading25 } = useQuery('posts', fetchLiveData);

...however the following error is thrown from this fetch.

Access to XMLHttpRequest at 'endpoint' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is this a localhost problem, or is some parameter being omitted from the axios fetch, or is it a permissions issue with the API provider? Or something else? How can we go about troubleshooting this issue?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I guess that the python env is outside the browser and the browsers block origin requestes and your client-side code don't have anything to do, the API is responsible of handling this issue by adding an http header "Access-Control-Allow-Origin: YOUR_DOMAIN OR *" and you said it's a paid API and you can't handle this so I assume that they have a console dashboard and I think in their settings you can configure ( whitelist ) the accepted origins and there you can add your origin, localhost or allow all origins.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try adding 'Access-Control-Allow-Origin': '*', in axios headers. Locally it should work. Good luck!

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