1

A similar question is already asked, but the suggested answer requires request.post, but "request" is now deprecated. There is no suggested on alternative methods. HTTP POST Google Cloud Functions NodeJS

Problem:

I've been looking for this for two days. I am simply looking for a bit of example code to send a POST request from a Google Cloud Function. I will pass a small bit of data and an auth token, but can't figure out the correct way to do it.

I will hit the API via HTTPS url and pass along the following parameters:

-d arg="1234" (a string)
-d access_token=0809809cx089009xci3 (an access token)

There are a million docs on how to trigger a cloud function from a POST, but nothing on how to actually generate the POST from within the cloud function.

Thanks in advance!

kjav
  • 93
  • 1
  • 7

1 Answers1

0

You can use libraries other than "request", for example: got, axios, or the default "HTTP" module in the standard node library. Checkout 5 Ways to Make HTTP Requests in Node.js.

Here is an implementation using "got":

const got = require('got');

const searchParams = new URLSearchParams([
  ['arg', '1234'],
  ['access_token', '0809809cx089009xci3'],
]);

got.post('https://example.com', { searchParams });
hangindev.com
  • 4,573
  • 12
  • 28
  • When testing the function on Google Cloud, I get error Cannot find module 'got'. How do I install a module in google cloud? – kjav Jun 10 '20 at 20:46
  • Could you check if "got" is listed as a dependency in "package.json" in the "functions" directory? You may also read about [Specifying dependencies in Node.js](https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs). – hangindev.com Jun 11 '20 at 03:54