Http post request with image to python webservice

Hi everybody. I am kind of become desperate on a task which seems quite easy at the first glace.

I want to call a webservice which is hosted on the same machine to analyze photos for faces etc.

The service is provided / started by a python script (which I have) and has very limited parameters etc. I also have a example how to call this services so everything seems to be straight forward.

Main challenge I have, is how to call the service with post method and handover a image (file). As the readme gives an example how to call the service using curl, I tried this and succeeded and I also succeed using postman. But I am failing using the "network / http request" node. I did also read a lot of posts and example but I always get the error message: "400 Bad Request Bad Request Missing file parameter!"

Curl example:
curl -X POST -F "file=@person1.jpg" http://localhost:8080/

What I have:

  • I have the image in a buffer

What I tried:

  • add the buffer to a variable image in the msg.payload -> failed
  • add the buffer to a variable image in the msg.payload.images array -> failed
  • add the buffer to a variable content in the msg.payload.images array in element 0 with fieldName=image content = buffer
  • ...

I believe I tried all combination which could be right but for me it looks like, that the approach with payload does not work because if a call with curl my own service in node red simulation the real service, the image is provided in a "req" object "files" array and the object inside has fieldname : "file" and a buffer variable

Finally I found this topic ...

... but the linked "solution" looks quite a hack to me.

Any other solutions out there?

Thanks

Do you have control of this webservice from a coding perspective or is it some stuff you have found somewhere? If possible, it would be much simpler if that image analyzing service had a MQTT interface

This is my flow get image from folder;
i call link https://il04.md:8080/test.jpg

And it's successful.

[
    {
        "id": "c7e341a0.381cc",
        "type": "http in",
        "z": "6fb9d512500305b6",
        "name": "",
        "url": "/:img",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 220,
        "y": 400,
        "wires": [
            [
                "cb8cf7f69fda838c"
            ]
        ]
    },
    {
        "id": "2fb1c354.d04e3c",
        "type": "file in",
        "z": "6fb9d512500305b6",
        "name": "",
        "filename": "",
        "format": "",
        "allProps": false,
        "x": 740,
        "y": 400,
        "wires": [
            [
                "c9e28681.361d78"
            ]
        ]
    },
    {
        "id": "c9e28681.361d78",
        "type": "change",
        "z": "6fb9d512500305b6",
        "name": "Set Headers",
        "rules": [
            {
                "t": "set",
                "p": "headers",
                "pt": "msg",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "headers.content-type",
                "pt": "msg",
                "to": "image/png",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1070,
        "y": 400,
        "wires": [
            [
                "88974243.7768c"
            ]
        ]
    },
    {
        "id": "88974243.7768c",
        "type": "http response",
        "z": "6fb9d512500305b6",
        "name": "",
        "x": 1250,
        "y": 400,
        "wires": []
    },
    {
        "id": "cb8cf7f69fda838c",
        "type": "function",
        "z": "6fb9d512500305b6",
        "name": "",
        "func": "// msg.image = msg.req.parms.img;\nif(msg.req.params.img != null){\n    msg.filename=`C:\\\\Users\\\\dumitru.maros\\\\.node-red\\\\publics\\\\`+msg.req.params.img;\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 400,
        "wires": [
            [
                "2fb1c354.d04e3c"
            ]
        ]
    }
]

maybe it's important to you set the header.

Hi Dumy,
thanks for your answer and your example flow. If I am correct, you just add the image buffer to the payload and that´s it, right?

"my" service expects a "file" parameter somehow in the payload

Hi krambriw,
so in theory I have full control. I found a docker image

and just use it. So I could do also write my own service for this image...

I also though about just calling curl...

vanTuxen

But then it is possible. There is an example written in python that uses a picam. Have you tried, does that work?

If so I would take that example and write another script that instead of the picam would receive the image data via mqtt, that would give you a simple interface to post images

Hi all,
thanks for your support. At the end I now call curl with exec node. maybe not the best approach but works pretty ok

  1. store file (buffer) to tmp
  2. create curl cmd (parameters etc.)
  3. call curl with exec node
  4. delete file
  5. work on curl response

if anybody needs to flow as an example, please ping me

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.