Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Manage HTTPs Request to external load balancer: delay and customise response

Hi,

We have an application behind a global external load balancer and we would like to add a condition, for which if an http request to a specific URL (API of the application behind the load balancer) is sent, then we do something in the background within the application, delaying the response to the API call, then sending a customised JSON response. How can we achieve this in GCP please?

Thanks,

3 2 172
2 REPLIES 2

Hi @pontorieroa,

You can use Cloud Load Balancing with a backend service that includes your application and a URL map to route requests to specific URLs. However, delaying the response and sending a customized JSON response within the application requires changes in the application code itself.

1. Set up an HTTP(S) Load Balancer with a backend service. Follow the guide on this documentation

2. Create a URL map by following this instruction. Then, add a URL map rule to match the specific URL (API) of your application.

3. Create a URL redirector to delay the response and send a customized JSON response as needed. 

4. Modify your application code to handle the specific URL (API) request and perform the desired background tasks. Also, implement the response delay and customized JSON response in the application code.

Below is my sample code snippet in Python using Flask to demonstrate the customized JSON response:

from flask import Flask, jsonify
import time

app = Flask(__name__)

@app.route('/api')
def api():
# Perform background tasks here
time.sleep(5) # Delay the response for 5 seconds
response = {
'message': 'Customized JSON response',
'status': 'success'
}
return jsonify(response)

if __name__ == '__main__':
app.run()  

 Let me know if this helps. 

Hi Marvin, 

Thanks for your response. The problem is that I can't change the application as it's a third party application.

Hence it would be hard to do that. We are looking at cloud function at the moment.

Antonella