Testing callable cloud functions with the Firebase CLI shell
Asked Answered
G

5

15

I have been trying new firebase callable cloud functions in firebase functions:shell I keep on getting following error

Request has incorrect Content-Type.

and

RESPONSE RECEIVED FROM FUNCTION: 400, {"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}

Here is ho w I am trying to call this function on shell

myFunc.post(dataObject)

I have also tried this

myFunc.post().form(dataObject)

But then I get wrong encoding(form) error. dataObject is valid JSON.

Update:

I figured that I need to use firebase serve for local emulation of these callable https functions. Data needs to be passed in post request like this(notice how its nested in data parameter)

{
 "data":{
    "applicantId": "XycWNYxqGOhL94ocBl9eWQ6wxHn2",
    "openingId": "-L8kYvb_jza8bPNVENRN"
 }
}

What I can't figure still is how do I pass dummy auth info while calling that function via a REST client

Grenier answered 6/4, 2018 at 6:55 Comment(1)
What you're trying to do is not really well supported at the moment. You have to understand the wire protocol for callable in order to manipulate them, and that's not yet been documented.Reflexion
C
21

I managed to get it working running this from within the functions shell:

myFunc.post('').json({"message": "Hello!"})

Crackerjack answered 6/4, 2018 at 11:49 Comment(2)
OMG thanks Merry for this tip. I got it working in functions v2 by wrapping the message in a 'data' object. myFunc.post('').json({"data": {"message": "Hello!"}})Daddylonglegs
You can do auth by myFunc.post('', {headers: {Authorization: 'Bearer $token'}}).json({'data': 'hello'})Latishalatitude
G
4

As far as I can tell, the second parameter to the function contains all of the additional data. Pass an object that contains a headers map and you should be able to specify anything you want.

myFunc.post("", { headers: { "authorization": "Bearer ..." } });

If you're using Express to handle routing, then it just looks like:

myApp.post("/my-endpoint", { headers: { "authorization": "Bearer ..." } });
Gamesmanship answered 14/8, 2018 at 13:6 Comment(0)
S
3

The correct syntax for the CLI has changed to myFunc({"message": "Hello!"})

Symbolism answered 27/5, 2020 at 20:3 Comment(1)
Welcome to Stackoverflow. Please try give a good description of your answer or how it works so that it would be helpful to others.Moonscape
A
2

If you take a look at the source code, you can see that it is just a normal https post function With an authentication header that contains a json web token, I'd recommend using the unit test api for https functions and mocking the header methods to return a token from a test user as well as the request body

[Update] Example

const firebase = require("firebase");
var config = {
  your config
};
firebase.initializeApp(config);
const test = require("firebase-functions-test")(
  {
    your config
  },
  "your access token"
);
const admin = require("firebase-admin");
const chai = require("chai");
const sinon = require("sinon");

const email = "[email protected]";
const password = "password";
let myFunctions = require("your function file");
firebase
  .auth()
  .signInWithEmailAndPassword(email, password)
  .then(user => user.getIdToken())
  .then(token => {
    const req = {
      body: { data: { your:"data"} },
      method: "POST",
      contentType: "application/json",
      header: name =>
        name === "Authorization"
          ? `Bearer ${token}`
          : name === "Content-Type" ? "application/json" : null,
      headers: { origin: "" }
    };
    const res = {
      status: status => {
        console.log("Status: ", status);
        return {
          send: result => {
            console.log("result", result);
          }
        };
      },
      getHeader: () => {},
      setHeader: () => {}
    };
    myFunctions.yourFunction(req, res);
  })
  .catch(console.error);
Archivolt answered 10/4, 2018 at 18:16 Comment(0)
M
0

I tried the json version and looks like that is not a function anymore.

myHttpsFunc.post(...).json is not a function

On firebase shell, since json is not a function, we should use the second parameter to pass what is needed. You can pass headers, body and probably other request fields.

I was able to call firebase functions in firebase functions:shell with payload using the second param.

myfunc.post('', {
  body: {
    key: "value",
    key2: "value"
  }
});

I really think that firebase team should update their docs, because I could not find a way to call it with json params.

Firebase Docs: https://firebase.google.com/docs/functions/local-shell

Miltonmilty answered 11/7 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.