Search code examples
azurenode-red

Node-red: function node to send a local image to Azure with Analyze Image API


I want to send a local image file to Azure cognitive service with Analyze Image API for image recognition in node-red. This is my nodes:

The all nodes

The code in function node is :

msg.payload = {"data" : "D:\TEMP2\tsaie.jpg"};
msg.headers = {
    "Ocp-Apim-Subscription-Key" : "439aa9b420e34cXXXXXXXXXXXX",
    "Content-Type" : "multipart/form-data"
}
return msg;

The HTTP-request node is POST and :

https://comvisonapi.cognitiveservices.azure.com/vision/v3.2/analyze?visualFeatures=Description,Faces

After I sent this request, I got errors:

msg.payload : string[168]
"{"error":{"code":"InvalidRequest","innererror":{"code":"InvalidImageFormat","message":"Input data is not a valid image."},"message":"Input data is not a valid image."}}"

Can you help me and what correct code should be in function node? Thank you very much!!


Solution

  • You can't just pass the filename as the data field. The HTTP-request node will just send that string, it will not load the content of the file from disk.

    You will need to use a file node to load the content of the image then build a properly formatted payload object. Here is an example:

    var fileData = msg.payload;
    msg.headers = {
      "Ocp-Apim-Subscription-Key" : "439aa9b420e34cXXXXXXXXXXXX",
      'Content-Type': 'multipart/form-data'
    };
    
    msg.payload = {
        'image' : {
            'value': fileData,
            'options': {
                'filename': 'image.png'
            }
        }
    };
    
    return msg;
    

    https://discourse.nodered.org/t/add-support-for-multipart-form-data-in-request-node/8111