繁体   English   中英

Flutter 发布请求标头不会出现在 Google Cloud 上 Function

[英]Flutter post request headers do not appear on 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'});

在谷歌云中,我print(request.headers)我看到一堆标头但没有授权或内容类型标头。

我应该怎么办?

PS 同样的问题在这里Flutter 调用 firebase 云 function admin.auth.updateUser但我不想使用可调用的 function

浏览器在 POST 之前发送了一个 OPTIONS 请求( 预检)。

我需要更改 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM