Can I use postman for onCall methods made as functions in firebase?
Asked Answered
M

2

29

So I've made this very simple method, where I want to sign in with to firebase using a custom token. As of right now it's simply

export const createCustomToken = functions.region('europe-west1').https.onCall(async (data, context) => {

    try {
        const firebaseJWT = await authenticationProvider.createCustomToken()
        console.log(firebaseJWT.toString())
    } catch (err) {
        throw new functions.https.HttpsError('internal', 'Something went wrong ');
    }
});

The authenticationProvider looks like this:

return firebase.admin.auth().createCustomToken("Some uid")
       .then((token) => {
           console.log("Did create custom token.");
           return token;
       }).catch((error) => {
           console.log("Error creating custom token:" + error);
           throw new firebase.functions.https.HttpsError('internal', 'createCustomToken(uid) has failed for some reason');
       }) 
}

I tried using the shell method to run it locally, doing this line in command prompt: firebase functions:shell and then calling authenticationController.createCustomToken({})

which just gives me this response: Error creating custom token:Error: Failed to determine service account. Make sure to initialize the SDK with a service account credential. Alternatively specify a service account with iam.serviceAccounts.signBlob permission. Original error: Error: Error while making request: getaddrinfo ENOTFOUND metadata metadata:80. Error code: ENOTFOUND

The service account seems to have the proper roles, so I don't think that's the issue.

So I was wondering if there was any way I could call me createCustomToken() function from Postman for instance, and see the response so I can test my code properly, without having an app implement the onCall method to see the results?

Or what am I missing to run this through the firebase functions:shell command?

Monti answered 22/2, 2019 at 8:42 Comment(1)
Postman isn't going to implement the protocol that callables use on top of HTTP: firebase.google.com/docs/functions/callable-referenceBatha
D
63

You can test firebase functions on Postman using request headers as described in official documentation here

First you'll have to serve your functions locally using following command at your project's root folder

firebase serve

You can also check official documentation how to serve functions locally here

On running above command you'll get an http link for your function. Something like this

http://localhost:5000/{YOUR_FIREBASE_PROJECT_NAME}/us-central1/myFunction

Use that link for POST request in Postman. In the "Body" section of Postman select "Raw" then "JSON (application/json)" from dropdown and type following sample of body in the body section

{
    "data": {}
}

Adding this json object with "data" argument in the body is required as mentioned in the documentation here. Check following screenshot for reference

Screenshot displaying sample body for requesting onCall method in Postman

Now you can send request to get response from your function. Hope it helps :)

Note: There is a mistyped "j" in the body of request in screenshot. Please ignore that.

Dupery answered 25/4, 2019 at 9:54 Comment(7)
There is no JSON(application/json) option in postman anymore. Is it equal to JSON option now?Lightyear
doesnt work for me. "message": "Bad Request", "status": "INVALID_ARGUMENT"Carricarriage
@Carricarriage same for meLightyear
@Carricarriage This is working for me-- did you add the { "data": {} } as a JSON body? I get that error when I don't pass an empty data objectDentist
i got this "The function must be called while authenticated." errorCarricarriage
I added "content-type"="application/json" in tab "header", then it worked for me as wellSestertium
be sure that you are sending a POST, not a GETLello
M
1

to request a callable method with postman you should make a "POST" request with:

  1. a "header" with (content-type : application/json ; charset=utf-8)
  2. a "body" with a json object that has "data" key like { "data":"any data " }

sorce: https://firebase.google.com/docs/functions/callable-reference

Materse answered 3/2 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.