@@ -118,3 +118,60 @@ def get_signed_url(request):
118118
119119 return url
120120# [END functions_http_signed_url]
121+
122+
123+ # [START functions_http_cors]
124+ def cors_enabled_function (request ):
125+ # For more information about CORS and CORS preflight requests, see
126+ # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
127+ # for more information.
128+
129+ # Set CORS headers for the preflight request
130+ if request .method == 'OPTIONS' :
131+ # Allows GET requests from any origin with the Content-Type
132+ # header and caches preflight response for an 3600s
133+ headers = {
134+ 'Access-Control-Allow-Origin' : '*' ,
135+ 'Access-Control-Allow-Methods' : 'GET' ,
136+ 'Access-Control-Allow-Headers' : 'Content-Type' ,
137+ 'Access-Control-Max-Age' : '3600'
138+ }
139+
140+ return ('' , 204 , headers )
141+
142+ # Set CORS headers for the main request
143+ headers = {
144+ 'Access-Control-Allow-Origin' : '*'
145+ }
146+
147+ return ('Hello World!' , 200 , headers )
148+ # [END functions_http_cors]
149+
150+
151+ # [START functions_http_cors_auth]
152+ def cors_enabled_function_auth (request ):
153+ # For more information about CORS and CORS preflight requests, see
154+ # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
155+ # for more information.
156+
157+ # Set CORS headers for preflight requests
158+ if request .method == 'OPTIONS' :
159+ # Allows GET requests from origin https://mydomain.com with
160+ # Authorization header
161+ headers = {
162+ 'Access-Control-Allow-Origin' : 'https://mydomain.com' ,
163+ 'Access-Control-Allow-Methods' : 'GET' ,
164+ 'Access-Control-Allow-Headers' : 'Authorization' ,
165+ 'Access-Control-Max-Age' : '3600' ,
166+ 'Access-Control-Allow-Credentials' : 'true'
167+ }
168+ return ('' , 204 , headers )
169+
170+ # Set CORS headers for main requests
171+ headers = {
172+ 'Access-Control-Allow-Origin' : 'https://mydomain.com' ,
173+ 'Access-Control-Allow-Credentials' : 'true'
174+ }
175+
176+ return ('Hello World!' , 200 , headers )
177+ # [END functions_http_cors_auth]
0 commit comments