POST rendered image to flask server via HTTP


I’m trying to send rendered frame outBMP (captured from RenderTarget) to a flask server via HTTP POST

TArray<FColor> outBMP;
outBMP.AddUninitialized(RT_Resource->GetSizeX() * RT_Resource->GetSizeY());
RT_Resource->ReadPixels(outBMP, ReadPixelFlags);

outBMP is the data i want to send to server. In Postman I can make HTTP Post image to server successfully as shown in image below.

I want to reproduce the same in UE4 C++. I’m not quite sure how to put all info present in red box (in screenshot) in C++ HTTP request. So far I’ve tried this

Step 1: Convert outBMP into image and then to string (there might some mistake here)

// Load image wrapper module
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

// Provide image wrapper with raw pixels (TArray<FColor>)
ImageWrapper->SetRaw(&outBMP[0], outBMP.Num() * sizeof(FColor), RT_Resource->GetSizeX(), RT_Resource->GetSizeY(), ERGBFormat::BGRA, 8);

TArray64<uint8> CompressedImage64 = ImageWrapper->GetCompressed(90);
FString CompressedData = BytesToString(CompressedImage64.GetData(), CompressedImage64.Num());

Step 2: Make HTTP Post request and set this image as content (getting 500 ERROR from flask server saying key error: ‘file’ which gives some hint)

TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->OnProcessRequestComplete().BindUObject(this, &ASceneCapture_Actor::OnResponseReceived);
HttpRequest->SetHeader("User-Agent", "X-UnrealEngine-Agent");
HttpRequest->SetHeader("Content-Type", "application/x-www-form-urlencoded");
HttpRequest->SetHeader("Content-Disposition", "form-data; name = \"file\"; filename=\"rendered_image.png\"; Content-Type=\"image/png\" ");

getting 500 ERROR from flask server saying KeyError:'file’. please help me, I’ve been struggling for days.

Log output : LogHttp: Warning: 0000017310DEB900: request has been successfully processed. URL: http://:5000/upload_image, HTTP code: 500, content length: 25472, actual payload size: 25472, elapsed: 3.14s

Hi again,

I tried to save rendered image locally and trying to send it to flask server.
i could do it in python, Please help me convert this python snippet into UE4 C++ format.

import requests

url = "http://<serverIP>:5000/upload_image"

headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)