Search code examples
functiongcloud

GCloud function call error: Error: could not handle the request


I have billing enabled for my project, however, I keep on getting an error returned:

Error: could not handle the request

And the traceback:


 Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
  File "/opt/python3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/opt/python3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/python3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)  

Here is my code:

import json
from google.cloud import storage
import requests

def call(request):
    
    url = "https://jsonplaceholder.typicode.com/todos/"

    response = requests.get(url)
    json_data = response.json()
    pretty_json = json.dumps(json_data)

    print(pretty_json)

With requirements being:

requests
google.cloud.storage 


Solution

  • The following should work for you:

    1. Create a virtualenv
    python3 -m venv venv
    source venv/bin/activate
    
    1. Install Requests (you're not using Google Cloud Storage)
    python3 -m pip install requests
    
    1. Script
    import json
    import requests
    
        
    url = "https://jsonplaceholder.typicode.com/todos/"
    
    response = requests.get(url)
    
    print(response.status_code)
    print(response.text)
    
    1. Execute the code
    python3 main.py
    
    1. Results
    200
    
    [
      {
        "userId": 1,
        "id": 1,
        "title": "delectus aut autem",
        "completed": false
      },
      ...
    ]