0

I am trying to locally test my callable Firebase cloud function.

I understand that this needs to be a post request and that there needs to be a Content-Type of application/json and a data object in the body of the request.

Here is my raw body in the request:

{
    "Content-Type": "application/json",
    "data": {
        "aString": "some string",
    }
}

In the response body I get:

{
    "error": {
        "message": "Bad Request",
        "status": "INVALID_ARGUMENT"
    }
}

And in the terminal I get:

>  {"severity":"WARNING","message":"Request has incorrect Content-Type. "}
>  {"severity":"ERROR","message":"Invalid request, unable to process."}

How do I format the body to get the right Content-Type?

Update:

Seems like the Content-Type used in Postman must be in the body and not the header:

enter image description here

The rest of the Postman request:

enter image description here

enter image description here

And my cloud function:

// eslint-disable-next-line import/no-unresolved
const functions = require('firebase-functions');
// eslint-disable-next-line import/no-unresolved
const connectyCube = require('connectycube');

exports.createSession = functions.https.onCall(async (data, context) => {
    if (context.auth) {
        const appId = functions.config().cc.appid;
        const authKey = functions.config().cc.authkey;
        const authSecret = functions.config().cc.authsecret;

        const CREDENTIALS = [
            {
                appId,
                authKey,
                authSecret,
            },
        ];

        connectyCube.init(...CREDENTIALS);

        const getSession = () => new Promise((resolve, reject) => {
            connectyCube.createSession((error, session) => {
                if (session) {
                    resolve(session);
                }
                console.error('FUNCTION ERROR', error)
                reject(error);
            });
        });
        const session = await getSession();
        return session;
    }
    return new Error('App authorization error');
});
Mr. Robot
  • 1,334
  • 6
  • 27
  • 79
  • What does your cloud function look like? Also can you share a screenshot of your postman request? – Dharmaraj Jun 25 '21 at 11:14
  • In Postman where you see the bold, blue "Text" next to the GraphQL option, if you click that and select JSON, it will set the header for you. Right now, you're sending plain text and manually adding the header as a solution. Postman takes care of that if you choose the appropriate type for the raw body. – I'm Joe Too Jun 25 '21 at 13:47

4 Answers4

0

try moving "content-type": "application/json" this into header and request again.

  • Thanks @Muhammed, I've updated the question as it seems like you can only update the content-type in the body. – Mr. Robot Jun 25 '21 at 11:50
0

If you want to directly call a Callable Cloud Function via an HTTPS request (instead of using one of the Client SDKs) your request needs to follow the Protocol specifications for https.onCall because "an https.onCall trigger for Cloud Functions is an HTTPS trigger with a specific format for the request and response".

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

You should pass the application/json in the headers and not in the body. enter image description here

This should automatically change the header to application/json in the headers tab. Please try switching to JSON from that dropdown menu and change your body to:

{
  "aString": "some string"
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
0

Self-answering but the reason this didn't work was because a custom header needs to be added in this specific way to the request in Postman:

https://stackoverflow.com/a/68040185/11584105

Mr. Robot
  • 1,334
  • 6
  • 27
  • 79