Issue
I have deployed a cloud function written in Python to GCP.
from flask import escape
import functions_framework
@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args
    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']
    else:
        name = 'World'
    return 'Hello {}!'.format(escape(name))
I am able to call this function from angular using axios for example. However, no data is sent through. If I print request.data in the python function it prints empty. Also request_json is always None.
axios.post('https://projektid.cloudfunctions.net/hello_http', { 
        data: 123
      })
      .then((result: any) => {
        console.log(result)
      })
      .catch((err) => {
        console.log('ERROR', err)
      });
Edit: It works fine and passes the arguments if I call it from Python like this:
import requests
r = requests.post('https://projektid.cloudfunctions.net/hello_http', json={'data': 123})
print(r.text)
How can I correctly pass data to this python function from angular/typescript?
Solution
If you're calling your function from localhost make sure you're handling cors requests. You can find more information about that in the documentation.
Answered By - Žan Ožbot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.