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

304
Visualizações
CORS AWS API-Gateway with Authentication Angular2

my AWS API-Gateway is configured as Lambda Proxy. In Lambda i added the following Headers:

'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key'

First, everythink works fine with it. Now i added an Authorizer with CognitoUserPool and i get the following Error:

XMLHttpRequest cannot load https://xxx.eu-west-1.amazonaws.com/xxx/xxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.

I think the problem is, that with Authentication my Lambdafunction is not invoked and so no Headers were set. I call the Endpoint from an Angular2 Application with

const headers = new Headers();
headers.append('Authorization', 'authtoken');
http.get('address...', headers);

But the headers were not set/append to the request because of any CORS-reasons i think.

Anyone an idea how can i solve this?

P.S.: I tried the "Enable Cors" feature of API-Gateway but that does not work.

Edit: I also have a "OPTIONS" Method in Gateway with Authentication, that responds with the Headers above.

Greetings

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Is your Lambda Proxy function mapping to the ANY HTTP method type? If so, it may be attempting to perform authentication on the pre-flight (OPTIONS) request. If this is the case, try mapping your proxy function to POST, GET, etc. specifically.

over 4 years ago · Santiago Trujillo Relatório

0

I found the solution of the problem. It was not caused by API-Gateway. Problem was this code:

const headers = new Headers();
headers.append('Authorization', 'authtoken');
http.get('address...', headers);

The "get" needs a "RequestOptions"-object. It has to be:

const headers = new Headers();
headers.append('Authorization', 'authtoken');
const options = new RequestOptions({headers: headers});
http.get('address...', options);
over 4 years ago · Santiago Trujillo 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