Skip to content

aws api gateway http api return 403 Forbinden on POST or OPTIONS method from angular app

0

I create api gateway http api with route ANY and bind to lambda function I don't need for now authorizers

api

I edit cors config cors api gateway works with postman(No Authorization) from angular app Get methods works but put, post, delete and delete doesn't call chrome

in angular app I use service with http

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { DeliveryOrder } from '../model/deliveryOrder';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { catchError, tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class OrderService {

  apiURL = environment.apiURL;
  constructor(private http: HttpClient) { }


 public getOrdersList(): Observable<DeliveryOrder[]> {
       return this.http.get<DeliveryOrder[]>(this.apiURL);
  }


 


 public createOrder(order: DeliveryOrder): Observable<DeliveryOrder> {
  console.log('Order to create:', order);  // Log the order being sent

  return this.http.post<DeliveryOrder>(this.apiURL, order).pipe(
    tap({
      next: (response) => {
        console.log('Response from API:', response);  // Log the successful response
      },
      error: (error) => {
        console.error('Error creating order:', error);  // Log any error that occurs
      }
    }),
    catchError((error) => {
      console.error('Error creating order:', error);  // Log the error again if needed
      throw error;  // Rethrow the error so it can be handled further up if needed
    })
  );
}

  public  updateOrder(order:DeliveryOrder): Observable<DeliveryOrder> {
    return this.http
      .put<DeliveryOrder>(this.apiURL + '/' + order.email, order);
  }

  public deleteOrder(email: string) {
    console.log('Order to delete:', email); 
    return this.http
      .delete<DeliveryOrder>(this.apiURL + '/' + email);
  }

}

logs work

1 Answer
-1

The 403 Forbidden error you're encountering with POST and OPTIONS methods from your Angular app to the AWS API Gateway HTTP API could be due to several reasons:

  1. CORS Configuration: Although you've edited the CORS configuration, ensure that it's properly set up for all HTTP methods you intend to use (GET, POST, PUT, DELETE, OPTIONS). Make sure the 'Access-Control-Allow-Methods' header includes all these methods.

  2. Lambda Function: Verify that your Lambda function is correctly handling all the HTTP methods, including OPTIONS for preflight requests. The function should return appropriate CORS headers for all requests.

  3. API Gateway Configuration: Double-check that your API Gateway is configured to pass all necessary headers to and from your Lambda function. This includes CORS headers.

  4. Angular HTTP Client: Ensure your Angular app is sending the correct headers with each request. You might need to add custom headers or modify existing ones.

  5. Browser Security: Some browsers might block certain types of requests due to security policies. Check your browser's console for any security-related messages.

  6. API Key or Authorization: Even though you mentioned not needing authorizers, verify that there isn't any unintended authorization or API key requirement set up on your API Gateway.

To troubleshoot:

  1. Use browser developer tools to inspect the exact request being sent and the response received.
  2. Check CloudWatch logs for your Lambda function to see if the requests are reaching it and how it's responding.
  3. Try adding explicit CORS headers in your Lambda function response.
  4. Ensure your Angular environment variables (apiURL) are correctly set.

If the issue persists after checking these points, you may need to review your API Gateway and Lambda configurations in more detail or consider reaching out to AWS support for further assistance.
Sources
Mock Api returning 403 Forbidden when calling it from a lambda function | AWS re:Post

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.