mokumoku-party / banmeshi

晩飯の在庫と料理を管理するアプリ
0 stars 1 forks source link

CORSエラーでFlutter Webから接続できない #11

Closed dicenull closed 11 months ago

dicenull commented 12 months ago
> curl \
          --header 'Content-Type: application/json' \
          --data '{"sentence": "I feel happy."}' \
          http://localhost:8080/InventoryService/FetchInventory
{"ingredients":[{"name":"hoge","amount":1}]}⏎

は成功

"github.com/rs/cors"が良いらしいのでサーバに追加してみて、接続

> go run cmd/main.go
[cors] 2023/09/13 22:55:45 Handler: Preflight request
[cors] 2023/09/13 22:55:45   Preflight aborted: headers '[Content-Type X-Grpc-Web X-User-Agent]' not allowed

エラー

dicenull commented 12 months ago

main.goでlocalhostからの通信とx-grpc-webを許可したら通信できた

    corsHandler := cors.New(cors.Options{
        AllowedOrigins:   []string{"http://localhost:*"},
        AllowCredentials: true,
        AllowedHeaders:   []string{"Content-Type", "X-Grpc-Web", "X-User-Agent"},
        // Enable Debugging for testing, consider disabling in production
        Debug: true,
    }).Handler(mux)

    http.ListenAndServe(
        "localhost:8080",
        corsHandler,
    )
dicenull commented 12 months ago

FlutterWebがDebugだと localhostのランダムなポートに立つので、"http://localhost:*" にした。 ポートとはいえアスタリスクはよくないはずなので、AllowedOriginはうまいこと設定したい