Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionscors

CORS Header missing from POST request in Google Cloud Function (Python)


I have the following Google Cloud function in Python:

`@functions_framework.http def main(request): # Enable CORS for all routes headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Headers': 'Content-Type' }

if request.method == 'OPTIONS':
    return ('', 204, headers)

# Call the appropriate function based on the HTTP method
if request.method == 'POST':
    return store_attendance(request)
else:
    return ('Method Not Allowed', 405, headers)`

This function calls the store_attendance function which essentially stores data into a MongoDB database from the request JSON.

Now whenever I try to send a request to this function, I get missing CORS origin header issue. The issue is - I found that the pre-flight request of OPTION has all the 'access-control' headers in the response headers, but the subsequent POST request was missing all the 'access=control' headers. Any clue on how to fix this?


Solution

  • I added the some access-control headers in my other store_attendance method and it worked as it was missing those headers.