0

I'm trying to use url paths in my google function and can't seem to access the body of the curl request, it returns None.

This is my code:

from flask import Flask, request, jsonify
import werkzeug.datastructures
from google.cloud import pubsub_v1
import json
import functions_framework
app = Flask(__name__)

@app.route('/')
def root():
    return 'Hello World!'

@app.route('/v1/dummy/<dummy_id>/notdummy/<notdummy_id>/validate', methods=['POST'])
def validate(dummy_id, notdummy_id):
    return {"function":"provision",
            "dummy_id":dummy_id,
            "notdummy_id":notdummy_id,
            "body":content_data}

def main(request):
    with app.app_context():
        global content_data
        content_data = request.get_json(silent=True)
        headers = werkzeug.datastructures.Headers()
        for key, value in request.headers.items():
            headers.add(key, value)
        with app.test_request_context(method=request.method, base_url=request.base_url, path=request.path, query_string=request.query_string, headers=headers, data=request.form):
            try:
                rv = app.preprocess_request()
                if rv is None:
                    rv = app.dispatch_request()
            except Exception as e:
                rv = app.handle_user_exception(e)
            response = app.make_response(rv)
            return app.process_response(response)

And my curl request to call the google function.

curl -X POST https://region-environment.cloudfunctions.net/function-name/v1/dummy/id1/notdummy/id2/validate -d {"key":"value"}
Tony Stark
  • 511
  • 2
  • 15
  • 1
    Did you try adding content type header in the curl request? `-H "Accept: application/json" -H "Content-Type: application/json"` – Dharmaraj Jun 22 '22 at 07:31
  • What is the value of the property **request.is_json**? – John Hanley Jun 22 '22 at 07:39
  • @JohnHanley It's True when I use the headers that Dharmaraj mentioned, False otherwise. – Tony Stark Jun 22 '22 at 07:48
  • Does specifying the correct header solve your problem? If yes, let @Dharmaraj know so that he can post the answer. – John Hanley Jun 22 '22 at 08:00
  • No it doesn't unfortunately @JohnHanley. And I'm really confused as to why it's not able to read request.get_json, it worked when I didn't have all the routing. And now I added that block of code using `werkzeug.datastructures` and it's tough for me to even understand. I'll do a deeper analysis and update my question later, will let you know after that, maybe you can have a look again.. – Tony Stark Jun 22 '22 at 08:11
  • 1
    Test your code locally before deploying to Cloud Functions. That will speed up your debugging. It should be simple to wrap a Flask server onto your application so that you can debug locally – John Hanley Jun 22 '22 at 08:14
  • I tried the sample HTTP function from the [documentation](https://cloud.google.com/functions/docs/writing/http#writing_http_content-python) and request body had data when `Content-Type` header was present of with `get_json(force=True)`. Can you the same function and try once? – Dharmaraj Jun 22 '22 at 08:34
  • I ended up implementing the function using global variable. But I'd love to know of a better method. Would appreciate it if you can have a look at it @JohnHanley :) – Tony Stark Jun 22 '22 at 17:54
  • @Dharmaraj Thank you, I used a similar method and implemented the same using global variable, but would love to know a better method. – Tony Stark Jun 22 '22 at 17:54

0 Answers0