8

My local function works fine running on

firebase serve --only functions

but once it is deployed to the cloud, I cannot make the same get request to it, using postman. I get the following error on stackdriver: Unexpected token u in JSON at position 0 at JSON.parse, and my request returns the following: 400. That’s an error.Your client has issued a malformed or illegal request. That’s all we know.

The Data I send in both local and firebase is a GET request of type application/json with a body of: { "data": { "Celebrity_A": "Brad Pitt", "Celebrity_B": "Angelina Jolie" } }

What request is it the firebase function expecting remotely, compared to on local?

Below is the start of my function:

// Main index.ts
exports.funct = functions.https.onRequest(functexp)

// functexp file
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as _request from 'request';
const serviceAccount = require('./credentials.json');
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();

export function functexp(request, response) {
    console.log(`request`);
    console.log(request);
    let celebName_A = null;
    let celebName_B = null;
    if(request !== undefined){
        celebName_A = request.body.data['Celebrity_A'];
        celebName_B = request.body.data['Celebrity_B'];
        console.log(`celebA is ${celebName_A}`)
    } etc...
}
daedsidog
  • 1,732
  • 2
  • 17
  • 36
  • 1
    Are you putting single quotes like that around your JSON? That's not right. – Doug Stevenson Dec 11 '18 at 17:38
  • Hey Doug, no that was just a formatting mistake on here, apologies – Tristram Tolliday Dec 11 '18 at 18:08
  • 1
    When you say "on hosting" are you implying that you are calling the function through Firebase Hosting [dynamic content](https://firebase.google.com/docs/hosting/functions)? If so, include your `rewrites` here. If not, I'd start with `credentials.json` which probably exists locally and not remotely. Note that you don't normally need that; you can just do `admin.initializeApp()` when running in Functions. – Kato Dec 11 '18 at 20:32
  • @Kato when I say hosting, I am referring to the fact it has been deployed to firebase cloud functions, as opposed to serving it from my local machine using the firebase cli. I can confirm that the config.json does exist remotely. – Tristram Tolliday Dec 12 '18 at 09:14
  • What if you send data as `POST` request? You don't see `GET` requests with body very often... – Karlo A. López Dec 12 '18 at 17:23
  • @KarloA.López Would you like to put that in as an answer, that has worked for me – Tristram Tolliday Dec 13 '18 at 15:36

2 Answers2

27

Try to send your request as POST method, you don't see GET requests with the body very often, and that's why POST is more secure, it's never cached and most importantly there's no size limit, maybe that's the reason cause your GET request is not working.

Hope it helps.

Sandeep Rana
  • 3,251
  • 2
  • 24
  • 38
Karlo A. López
  • 2,548
  • 3
  • 29
  • 56
  • 1
    That fixed it. Is it common to use post to request data with parameters in the body? My parameters contained private data so I thought it would be best to put them in the request body. – MadMac Aug 26 '21 at 23:35
2

If you land here like me because a 400 error when trying a GET call using POSTMAN a warning: be sure to not have anything in the body (I switch between POST and GET when testing and forget to se body to none)