-1

put req screenshot Hi I am currently trying to parse the following post request in a python

-------------------------------28947758029299
Content-Disposition: form-data; name="eventData"; filename="eventData.txt"
Content-Type: application/json; charset=utf-8


    {
        "Rule" : "sendSpots",
        "Alert" : [
            {
                "Event" : {
                    "Version" : "1",
                    "EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
                    "Type"      : "Vision",
                    "Subtype"   : "Alarm",
                    "SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
                    "Origin"     : "DVS-BOX-PC",
                    "InstanceType" : "ParkingSpaceAnalyzer",
                    "InstanceName" : "ParkingSpotAnalyser",
                    "SubName"      : "P01",
                    "FrameTime"    : "2018-04-19T09:59:41.0970000Z",
                    "Parameters"   : [
                        {
                            "Name" : "DURATION",
                            "Type" : "int",
                            "Value" : "3669869"
                        },
                        {
                            "Name" : "STREAM_RESOLUTION",
                            "Type" : "string",
                            "Value" : "1280,720"
                        },
                        {
                            "Name" : "GENERIC_XML",
                            "Type" : "string",
                            "Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
                        },
                        {
                            "Name" : "REGION",
                            "Type" : "string",
                            "Value" : "638,613 694,377 792,373 695,614"
                        }
                    ]
                }
            }
        ]
    }

-------------------------------28947758029299--

I wish to extract Event as a dictionary such that I can access the version value, the Subname value and SensorUUId. To attempt to extract this info I have used the code

data = request.form
body = data['-------------------------------28947758029299\nContent-Disposition: form-data; name']

However this returns the below as type unicode:

"eventData"; filename="eventData.txt"
Content-Type: application/json; charset=utf-8


{
    "Rule" : "sendSpots",
    "Alert" : [
        {
            "Event" : {
                "Version" : "1",
                "EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
                "Type"      : "Vision",
                "Subtype"   : "Alarm",
                "SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
                "Origin"     : "DVS-BOX-PC",
                "InstanceType" : "ParkingSpaceAnalyzer",
                "InstanceName" : "ParkingSpotAnalyser",
                "SubName"      : "P01",
                "FrameTime"    : "2018-04-19T09:59:41.0970000Z",
                "Parameters"   : [
                    {
                        "Name" : "DURATION",
                        "Type" : "int",
                        "Value" : "3669869"
                    },
                    {
                        "Name" : "STREAM_RESOLUTION",
                        "Type" : "string",
                        "Value" : "1280,720"
                    },
                    {
                        "Name" : "GENERIC_XML",
                        "Type" : "string",
                        "Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
                    },
                    {
                        "Name" : "REGION",
                        "Type" : "string",
                        "Value" : "638,613 694,377 792,373 695,614"
                    }
                ]
            }
        }
    ]
}
-------------------------------28947758029299--

I do not know how to go any further, cheers.

Daniel
  • 169
  • 1
  • 4
  • 10

2 Answers2

0

You need to convert your posted json data to a dictionary:

json_dict = request.get_json()

Which you then need to parse to get the correct information:

UUID = json_dict['Alert'][0]['Event']['SensorUUID']

EDIT: minimal working example:

CURL

curl -X POST -H "Content-Type: application/json" -d '{
    "Rule" : "sendSpots",
    "Alert" : [
        {
            "Event" : {
                "Version" : "1",
                "EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
                "Type"      : "Vision",
                "Subtype"   : "Alarm",
                "SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
                "Origin"     : "DVS-BOX-PC",
                "InstanceType" : "ParkingSpaceAnalyzer",
                "InstanceName" : "ParkingSpotAnalyser",
                "SubName"      : "P01",
                "FrameTime"    : "2018-04-19T09:59:41.0970000Z",
                "Parameters"   : [
                    {
                        "Name" : "DURATION",
                        "Type" : "int",
                        "Value" : "3669869"
                    },
                    {
                        "Name" : "STREAM_RESOLUTION",
                        "Type" : "string",
                        "Value" : "1280,720"
                    },
                    {
                        "Name" : "GENERIC_XML",
                        "Type" : "string",
                        "Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
                    }' http://127.0.0.1:5000/ 

Python:

from flask import Flask, render_template,request, render_template_string
app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def homepage_process():
    if request.method == 'POST':
        json_dict = request.get_json()
        return json_dict['Alert'][0]['Event']['SensorUUID']

Response:

!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!
Joost
  • 3,609
  • 2
  • 12
  • 29
  • Thanks for your comment json_dict = request.get_json() returns nonetype – Daniel Apr 19 '18 at 12:05
  • Then you're not posting the form correctly. Can you show the code which posts the json? – Joost Apr 19 '18 at 12:12
  • Hi thank you for all your help. I am using a software called kiwiserver which sends regular post requests automatically. This is the header { "VERSION": "HTTP/1.1", "CONNECTION": "close", "ACCEPT-ENCODING": "gzip", "ACCEPT": "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml", "USER-AGENT": "RestSharp/105.2.3.0", "CONTENT-TYPE": "multipart/form-data; boundary=-----------------------------28947758029299", "CONTENT-LENGTH": "1780" } – Daniel Apr 19 '18 at 12:24
  • Change the `CONTENT-TYPE` in your header to `application/json` – Joost Apr 19 '18 at 12:26
  • I have edited to include a screenshot of the request in putreq. – Daniel Apr 19 '18 at 12:29
  • I cannot change the header content as the post request is uneditable. – Daniel Apr 19 '18 at 12:31
  • You can try `request.get_json(force=True)`. Otherwise I'm out of idea's, since I don't know how putreq works. But the flask bit is working perfectly in my example, so that's not the problem. – Joost Apr 19 '18 at 12:37
  • I have tried force=True also. It works for me with the curl request aswell. The problem seems to be in the example the kiwiserver is sending, Thank you though – Daniel Apr 19 '18 at 12:40
  • You can try debugging to see where your json data is stored by: `print(request.data)`, `print(request.__dict__)`, `print(request.files)`, `print(request.form)`, and see if you get something sensible. – Joost Apr 19 '18 at 12:42
0

I resolved the issue using the below code. By making the request body a string and parsing it. Thanks for the help.

data = request.stream.read()
first_s = data.find('{')
last_s = data.rfind('}')
data = data[first_s:last_s+1]
data = json.loads(data)
data = dict(data)
Daniel
  • 169
  • 1
  • 4
  • 10