Next.js Discord

Discord Forum

CORS policy while trying to connect to a docker container

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I am trying to connect to my "fake " lambda function (a local docker container built in python)
i tried curl , connecting with postman and i even changed the origin in postman into (localhost :300) and all of them are working
but when i try to post from my nextjs application i get a cors policy error!
Why is that happenening and how can i solve it?

11 Replies

Transvaal lionOP
Done actually but it is still the same error
did you allow that origin to make request?
@@ts-ignore did you allow that origin to make request?
Transvaal lionOP
I guess so....I'm not really a python developer but I think it does (as it is mimicking AWS lambda)
@Transvaal lion I guess so....I'm not really a python developer but I think it does (as it is mimicking AWS lambda)
I am also not a python dev but as you're saying that you setup cors so ig the allowed origins will be the culprit
Transvaal lionOP
I tried to add some cors policy in the API (helped by chatGPT ) tho nothing happened
can you show some code where you setup cors
Transvaal lionOP
Gimme a minute
Transvaal lionOP
this is the my function
def lambda_handler(event, context):
    
    try:

        PDFfiles = event.get('PDFfiles')
        persona = event.get('persona')
        product_name = event.get('product_name')
        response = event.get('response')
        language = event.get('language')
  
        processing_instance = Processing()
        return {
            'statusCode': 200,
            'headers': {
                'Access-Control-Allow-Headers': 'Content-Type',
                'Access-Control-Allow-Origin': 'http://localhost:3000',
                'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
            },
            'body': json.dumps({'result':processing_instance.process(PDFfiles, persona, product_name, response, language)})
        }

    except Exception as e:
        logger.error(f"Lambda handler error: {str(e)}")
        return {
                 'statusCode':500,
                 'body': 'Error'
            }

i have been noticing it is in return but that's what the docs have said and ain't that pytho dev to discuss that : https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html