[英]Flutter post request headers do not appear on Google Cloud Function
I am doing a post request in flutter to a google cloud function:我正在 flutter 中向谷歌云 function 发送帖子请求:
final uri = Uri.parse(
'https://example.cloudfunctions.net/send_to_queue');
final bearer = 'Bearer ${await user.getIdToken()}';
final response = await http.post(uri, body: json.encode(data),
headers: {HttpHeaders.authorizationHeader: bearer, 'Content-Type': 'application/json'});
In the Google Cloud I print(request.headers)
I see a bunch of headers but no Authorization or Content-Type headers.在谷歌云中,我
print(request.headers)
我看到一堆标头但没有授权或内容类型标头。
What should I do?我应该怎么办?
PS Same issue in here Flutter calling firebase cloud function admin.auth.updateUser but I don't want to use a callable function PS 同样的问题在这里Flutter 调用 firebase 云 function admin.auth.updateUser但我不想使用可调用的 function
The browser was sending an OPTIONS request ( preflight ) before the POST.浏览器在 POST 之前发送了一个 OPTIONS 请求( 预检)。
I needed to change the Google Cloud Function to handle this:我需要更改 Google Cloud Function 来处理这个问题:
def main(request):
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Get token from request
token = request.headers.get('Authorization').split('Bearer ')[1]
etc..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.