Evorlor
Evorlor

Reputation: 7562

How do I request a response from Firebase Functions?

I am new to Firebase Functions and TypeScript. I am trying to make a request from the client (Unity and C#) to the server (Firebase Functions and TypeScript).

My server logs show a 200 status code with no warning or errors. However, when I receive the response, it faults.

Client Code:

public void CallServer(UnityAction<string> callback)
{
    var function = MyFirebaseFunctions.GetHttpsCallable("myFunction");
    function.CallAsync().ContinueWith((response) =>
    {
        if (response.IsFaulted)
        {
            Debug.LogError("Fault!"); //Faults every time
        }
        else
        {
            string answer = response.Result.Data.ToString();
            Debug.Log(answer);
            callback(answer);
        }
    });
}

Server Code:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();

export const myFunction = functions.https.onRequest((request, response) =>
{
    response.send('{json:here}');
});

I do not know why it is faulting, nor do I know how to find more information. If I try to debug response, my IDE just says no. Can anyone with some more experience explain to me what I am doing wrong?

Upvotes: 0

Views: 1190

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

You are actually mixing up "standard" HTTP Cloud Functions with HTTP Callable Cloud Functions.

Your Cloud Function code ("Server Code") is "standard" HTTP Cloud Functions code, with

... functions.https.onRequest((request, response) => {...})

While your Unity code is calling an HTTP Callable Cloud Function, with

var function = MyFirebaseFunctions.GetHttpsCallable("myFunction");

The best way is to adapt the Cloud Function code as follows:

export const myFunction = functions.https.onCall((data, context) => {
    return {foo: 'bar'};
});

See the doc for more details.

Upvotes: 2

Related Questions