0
votes

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!

1

1 Answers

0
votes

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 });