calling an api service from flask with a callback response

  ajax, flask, python

How do I capture a response from calling an API that is making that response to a specific callbackurl for the client to see?

Suppose I am making an ajax post to some backend url in flask:

$.ajax({
   type: 'POST',
   url: 'api-req',
   data: {'d1': '{{ d1 }}', 'd2': d2},
   success: function(data) {
      console.log(data); //should do something else here
   }
});

So far so good, the url is making a requests post to some API service:

@public.route('/api-req', methods=['GET', 'POST'])
def api_req():

    if request.method == 'POST':
        d1 = request.form['d1']
        d2 = request.form['d2']

        api_url = f'{base_url}/api'
        api_data = {"d1": d1, "d2": d2, "callbackUrl": "http://www.myurl.com/api-service/response"}

        r = requests.post(sign_url, json=sign_data)

    return r.text # should return something else

@public.route('/api-service/response', methods=['POST'])
    def api_resp():
        # do something here to capture and send to client which is waiting 
        pass

But now I cannot figure out how to capture the response from the API service and send that to the client frontend to move on.

Source: Python Questions

LEAVE A COMMENT