簡體   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