Created
September 11, 2024 15:28
-
-
Save dfar-io/9a3a8cad9cf4b10d047c5379cfad689c to your computer and use it in GitHub Desktop.
Calls an external URL using Cloud Run Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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