Karthik Mannepalli
Karthik Mannepalli

Reputation: 63

unable to access invoke firebase http end point

I am writing a firebase function and trying to make it available as an HTTP endpoint. But when I try to access I get the error "Your client does not have permission to get URL /getDetails from this server."

const functions = require('firebase-functions');

const cors = require('cors');

var express = require('express')
var app = express();


'use strict';
app.use(cors);
app.get('/getDetails', function (req, res) {

res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

exports.app = functions.https.onRequest(app);

I expect the output {"fName":"Karthik","lName":"Mannepalli"} but what I get is Error: Forbidden

But the following code gives me the right output. In the below code I am not using express

exports.getDetails = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

Upvotes: 0

Views: 345

Answers (2)

zkohi
zkohi

Reputation: 2520

See https://firebase.google.com/docs/functions/http-events .

    exports.date = functions.https.onRequest((req, res) => {
      // ...
    });

For example, the URL to invoke date() looks like this:

https://us-central1-<project-id>.cloudfunctions.net/date

    const express = require('express');
    const cors = require('cors');
    
    const app = express();
    
    // Automatically allow cross-origin requests
    app.use(cors({ origin: true }));

    // Add middleware to authenticate requests
    app.use(myMiddleware);
    
    // build multiple CRUD interfaces:
    app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
    app.post('/', (req, res) => res.send(Widgets.create()));
    app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
    app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
    app.get('/', (req, res) => res.send(Widgets.list()));
    
    // Expose Express API as a single Cloud Function:
    exports.widgets = functions.https.onRequest(app);

For example, the URL to invoke the getter in the Express app example above looks like this:

https://us-central1-<project-id>.cloudfunctions.net/widgets/<id>

So in case of exports.app = functions.https.onRequest(app);, the get URL is /app/getDetails. Not /getDetails.

And in case of exports.getDetails = functions.https.onRequest(async (req, res), the get URL is /getDetails.

Upvotes: 1

nachshon f
nachshon f

Reputation: 3680

The code for your cloud function is not looking familiar to me at all. I'm not sure which tutorial or documentation you're using, but it should be as simple as this...

const functions = require('firebase-functions');

exports.SomeFunction = functions.https.onRequest((req, res) => {
   //Your function code
   res.status(200).end();
});

Try that code and let me know if you get any more errors.

Upvotes: 0

Related Questions