1

I'm having a problem with my Firebase Functions https request.

This is my code to trigger it:

  const express = require('express');
  const bodyParser = require('body-parser');
  const app = express();
  app.use(bodyParser.json());
  app.use(function(req, res, next) {
    res.setHeader('Content-Type', 'application/json');
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods", "GET, POST");
    next();
  });
  app.use((err, req, res, next) => {
    if (err instanceof SyntaxError) {
      return res.status(400).send();
    };

    next();
  });
  app.post('/fetchPosts', (req, res) => {
    exports.fetchPosts(req, res);
  });

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

  const cors = require('cors')({ origin: true })
  exports.fetchPosts = (req, res) => {
      console.log(req.body)
      console.log(res)
    let topics = req.body.topics || ['live'];
    let start = req.body.start || 0;
    let num = req.body.num || 10;
    let next = start+num;
    // setting up the response.
  });

That looks good as far as I can tell..

Now when I do my api call I do:

   var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Content-Type', 'application/json; charset=UTF-8');
    const request = new RequestOptions({ headers: headers });
    const url = 'https://my-link.cloudfunctions.net/widgets/fetchPosts';
    let payload = {
        topics: ["live", "pets"]
    }
    console.log(JSON.stringify(payload))
    this.http.post(url, JSON.stringify(payload), request)
    .pipe(map((res:Response) => {
      console.log(res.json())
    }))
    .subscribe(
      data => console.log(data),
      err => console.log(err),
      () => console.log('Got feed')
    );

and it just returns topics with just ['live'].. because of the failsafe that I set up on the backend.. but why isn't getting my topics that I'm sending?

Also, when I console.log(req.body) on the backend it just shows {}.. an empty object..

Any ideas why the req.body doesn't seem to work? I do it with start and num as well, but they all revert back to the failsafe.

Simon
  • 2,498
  • 3
  • 34
  • 77

1 Answers1

0

You must use POST method to handle req.body . In your case, you can handle your variable with req.query

To handle req.body . You can use Postman, then select POST method and post data as JSON . You can read more to use Postman well.

Le Quang
  • 515
  • 4
  • 10
  • I updated my code so that it's a `post`.. but I'm still having the same problem.. any ideas? – Simon Jun 29 '19 at 20:40
  • You can try : ` app.post('/fetchPosts', (req, res) => { console.log(req.body) // to handle request }); ` – Le Quang Jun 30 '19 at 17:39
  • 1
    I had to call post like this in the firebase shell to get req.body to have data: `createPaymentIntent.post().form({amount: 777, currency: "usd"})` – dazza5000 Mar 23 '20 at 14:52