1

New to FastAPI and setting up a POST route in which I will want to accept the below request from an IP camera. I don't know how to set up the Pydantic model in my API class (like which data types and fields to "expect"). Does it have to be super specific, or is there like a "i'll accept anything" and then parse it accordingly, kinda like Flask does?

POST /alpv/receive.php HTTP/1.1\r\n
Host: 10.13.24.13\r\n
Content-Length: 7160\r\n
Content-Type: multipart/form-data; boundary=------------------------3df4e8eee91da308\r\n
--------------------------3df4e8eee91da308
Content-Disposition: form-data; name="event";
filename="20190204103017_10536event_7000175.json" Content-Type: application/octet-stream
{
  "packetCounter":"11890300",
  "capture_timestamp":"1623405938252",
  "frame_timestamp":"0",
  "capture_ts":"1623405938252000000",
  "datetime":"20210611 130538252",
  "plateText":"\u0052\u004c\u0045\u0031\u0031\u0036\u0038\u0034",
  "plateUnicode":"\u0052\u004c\u0045\u0031\u0031\u0036\u0038\u0034",
  "plateUTF8":"RLE11684",
  "plateASCII":"RLE11684",
  "plateCountry":"POL",
  "plateConfidence":"0.719034",
  "carState":"lost",
  "roiID":"2",
  "geotag":{
    "lat": 50.418114,
    "lon": 30.476213
  },
  "imageType": "plate",
  "plateImageType": "jpg",
  "plateImageSize": "0",
  "carMoveDirection":"in",
  "timeProcessing":"0",
  "plateCoordinates":[1185, 532, 140, 28],
  "plateCoordinatesRelative":[1030, 196, 140, 28],
  "carID":"1144696",
  "GEOtarget":"Camera",
  "imagesURI":[
    "/local/fflprapp/tools.cgi?action=getImage&name=45/20210611130538_785523lp_RLE11684_11890182.jpg",
    "/local/fflprapp/tools.cgi?action=getImage&name=44/20210611130538_547764roi_RLE11684_11890181.jpg"
  ],
  "imageFile":"/var/spool/storage/SD_DISK/fflprapp/images/44/20210611130538_547764roi_RLE11684_11890181.jpg",
  "camera_info":{
    "SerialNumber":"ACCC8ED3290D",
    "ProdShortName":"AXIS P1445-LE-3",
    "MACAddress":"AC:CC:8E:D3:29:0D"
  },
  "sensorProviderID":"defaultID_176"
}
--------------------------3df4e8eee91da308
Content-Disposition: form-data; name="image";
filename="20190204103016_999776lp_LBE397_7000175.png" Content-Type: application/octetstream
<IMAGE DATA>
--------------------------3df4e8eee91da308--

Some additional info. Here is what I was able to capture from PHP which was in the $_FILES array:

Array
(
    [event] => Array
        (
            [name] => 0001_20230406135945_186591_event_80947_106_261.json
            [full_path] => 0001_20230406135945_186591_event_80947_106_261.json
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpyvTuvu
            [error] => 0
            [size] => 1372
        )
    [image] => Array
        (
            [name] => 20230406135944_916307roi_HKZ7920_80945.jpg
            [full_path] => 20230406135944_916307roi_HKZ7920_80945.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpeDudVt
            [error] => 0
            [size] => 813226
        )
)

I then had to do a file_get_contents (php) to that tmp_name url to fetch the actual JSON output above.

The UploadFile data type produces a 422 Unprocessable Entity error.

Chris
  • 18,724
  • 6
  • 46
  • 80
Source Matters
  • 1,110
  • 2
  • 15
  • 35
  • This seems to just be two uploaded files? There is no need for a Pydantic model to describe that - use `async def endpoint_name(event: UploadFile, image: UploadFile)` to receive both as uploaded files. You can then load the json from `event` as you'd decode any other JSON: `json.load(event.file)`. If you want to use Pydantic validation to validate the message, you'll have to describe it as a Pydantic model or generate one from a JSON schema: https://docs.pydantic.dev/datamodel_code_generator/ – MatsLindh Apr 07 '23 at 22:01
  • `json.load(s)` should also be safe: https://stackoverflow.com/questions/38813298/is-json-loads-vulnerable-to-arbitrary-code-execution – MatsLindh Apr 07 '23 at 22:02
  • Related answers that you might find helpful can be found [here](https://stackoverflow.com/a/70657621/17865804), as well as [here](https://stackoverflow.com/a/73443824/17865804) and [here](https://stackoverflow.com/a/70636163/17865804) – Chris Apr 08 '23 at 05:08

0 Answers0