I am new to using GCP Cloud Functions and I am trying to deploy a Cloud Function using Python, that when called it will perform a get request and return some data. To do that I am using the requests.get() function of the Requests module. When calling the function I get the following error:
NameError: name 'requests' is not defined
After updating the code to include import requests in the main.py file and calling the function I get the error:
import requests ModuleNotFoundError: No module named 'requests'
I then tried to include it in the requirements.txt file as
requests==2.*
and received the 'no module' error. Below is the code I'm trying to run.
def web_request (requests):
data = requests.get_json()
if data['parameter'] == 'input':
GET_request = requests.get('RequestURL')
GET_data = GetRequest.json()
return GET_data
Please advise on the best resolution.
Here's an example that mirrors your code (see caveats below)
requirements.txt:
functions-framework==3.*
requests==2.*
main.py:
import functions_framework
import requests
@functions_framework.http
def web_request (request):
# request -- is the parameter
data = request.get_json()
if data['parameter'] == 'input':
# requests -- is the imported package
response = requests.get(data['url'])
data = response.json()
return data
NOTE The above code is a reflection of yours. It contains no error-handling.
python3 -m venv venv
source venv/bin/activate
python3 -m pip install requirement=requirements.txt
In one shell, start the server:
functions-framework --target web_request
In another shell, test the server:
curl
--request POST \
--header "Content-Type: application/json" \
--data '{"parameter":"input","url":"http://ip.jsontest.com/"}' \
http://localhost:8080/web_request
Should yield something like:
{"ip":"11.22.33.44"}
BILLING=[YOUR-BILLING]
PROJECT=[YOUR-PROJECT]
REGION=[YOUR-REGION]
gcloud projects create ${PROJECT}
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
# Deploy
gcloud functions deploy web_request \
--runtime=python310 \
--trigger-http \
--allow-unauthenticated \
--region=${REGION} \
--project=${PROJECT}
# Test
gcloud functions call web_request \
--project=${PROJECT} \
--region=${REGION} \
--data='{"parameter":"input","url":"http://ip.jsontest.com/"}'
Yields e.g.:
{"ip":"2600:1900:2000:13::14"}
Or:
TOKEN=$(\
gcloud auth print-identity-token)
ENDPOINT=$(\
gcloud functions describe web_request \
--region=${REGION} \
--project=${PROJECT} \
--format="value(httpsTrigger.url)")
curl --request POST \
--header "Authorization:bearer ${TOKEN}" \
--header "Content-Type:application/json" \
--data '{"parameter":"input","url":"http://ip.jsontest.com/"}' \
${ENDPOINT}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With