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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
  • 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. – Doug Stevenson Apr 13 '18 at 16:01

4 Answers4

21

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

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

  • 3
    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!"}}) – bdombro Aug 18 '18 at 11:59
  • 4
    You can do auth by `myFunc.post('', {headers: {Authorization: 'Bearer $token'}}).json({'data': 'hello'})` – Kyle Fang Sep 26 '18 at 09:10
3

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 ..." } });
Douglas Manley
  • 1,093
  • 13
  • 18
3

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

Carter
  • 1,184
  • 11
  • 5
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 = "test@test.test";
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);
raycar5
  • 31
  • 2
  • 3