Skip to content

Instantly share code, notes, and snippets.

@dfar-io
Created September 11, 2024 15:28
Show Gist options
  • Save dfar-io/9a3a8cad9cf4b10d047c5379cfad689c to your computer and use it in GitHub Desktop.
Save dfar-io/9a3a8cad9cf4b10d047c5379cfad689c to your computer and use it in GitHub Desktop.
Calls an external URL using Cloud Run Functions
package.json
============
{
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0",
"crypto-js": "4.2.0",
"node-fetch": "2.6.1"
}
}
index.js
========
const functions = require('@google-cloud/functions-framework');
const CryptoJS = require("crypto-js");
const fetch = require("node-fetch");
functions.http('makeRequest', async (req, res) => {
const url = 'URL_TO_SEND_REQUEST_TO'
const headers = new Headers();
// any headers you might want
// headers.append("Authorization", authHeader);
const externalRes = await fetch(url, {
headers: headers
})
.then(r => r.json().then(resJson => ({status: r.status, body: resJson})))
.then(obj => res.send(obj));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment