-1

This is my first time asking a question on this platform.

I currently have a camera set up that we are using for people counting. I am using a push function on the camera to push the data I need to a https url (I am just using local host on my pc). I am getting a response but the response is coming in as what seems to be a CombinedMultiDict which is nothing like the JSON payload documented/expected. Does anyone know how I can pull the "In" and "Out" variables so that I can further work with these variables.

from flask import Flask, request, jsonify
from paho.mqtt import client as mqtt_client
from werkzeug.datastructures import MultiDict

app = Flask(__**name**__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET','POST'])

def home():
       print(request.values)
       return "Camera"

app.run(host='localhost', port=777)

and once the camera pushes this is the response I get when I print request.values:

CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('{"Source":{"ReportTime":"2022-03-25T16:02:12Z","GroupID":"Group 01","DeviceID":"","ModelName":"SC9133","MacAddress":"00:02:D1:99:DB:13","IPAddress":"192.168.147.172","TimeZone":" 2","DST":"0"},"Data":[{"RuleType":"Counting","CountingInfo":[{"EndTime":"2022-03-25T15:54:00Z","In":0,"Out":0,"StartTime":"2022-03-25T15:53:00Z","RuleName":"Rule-1"}]}]}', '')])])

So I am seeing the in and out variable but I would like to isolate these variables in there own variable if that makes sense. I know this is going to be hard to test as this is a local camera but any advise would be appreciated.

warped
  • 8,947
  • 3
  • 22
  • 49
Alex Robus
  • 11
  • 2

1 Answers1

2

Try using request.form to specifically get the data. request.values returns both request.form and request.args. This previous answer covers this well.

If you proceed with request.form, here's one way you can achieve your goal:

import json

@app.route('/', methods=['GET','POST'])
def home():
    content = request.form  # ImmutableMultiDict(text, "")
    text = list(content.keys())[0]  # '{"Source":{"ReportTime":"2022-...
    json_content = json.loads(text)
    print(json_content["Data"][0]["CountingInfo"][0])
    return "Camera"

Output:

{'EndTime': '2022-03-25T15:54:00Z',
 'In': 0,
 'Out': 0,
 'StartTime': '2022-03-25T15:53:00Z',
 'RuleName': 'Rule-1'}
Tim
  • 248
  • 2
  • 7