I had already read this excellent answer: Do Google Cloud Platform HTTP Functions Support Route Parameters? for the server side
However there is no indication how to call a route from within Flutter app.
My current (not working) attempt in Flutter (client side):
import 'package:cloud_functions/cloud_functions.dart';
//...
Future<Null> callGoogleCloudFunctions() async {
FirebaseFunctions functions = FirebaseFunctions.instance;
try {
HttpsCallable callable = functions.httpsCallable('routes/fruits');
final results = await callable.call();
final fruits = results.data; // ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"]
print(fruits);
} catch (e) {
print('caught exception $e');
}
}
On server side (google cloud functions):
const app = express();
app.use(bodyParser.json());
app.use(cors({ origin: true }));
// configure the routes that the app will handle.
routesConfig(app)
export const routes = functions.https.onRequest(app);
export function routesConfig(app: Application) {
app.get('/fruits', [
fruits
]);
export async function fruits(req: Request, res: Response) {
try {
console.log("Hello logs!", {structuredData: true});
return res.status(200).send({"fruits": ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"] })
} catch (err) {
return handleError(res, err)
}
}
NB: I have user authentification but simplified the code for MRE
With Postman I have no trouble to call https://us-central1-{{PROJECT_ID}}.cloudfunctions.net/route/fruits and get the returned string so I know my problem is only on the client side
I don't want to use http post / get functions because cloud function is very convenient when it comes to authentication:
The request to the Cloud Functions backend made by this method automatically includes a Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase Auth, an auth ID token for the user is also automatically included.
Edit: Just succeeded using http package... still curious about the google cloud function plugin?