Hey everyone,
I am trying to create an http post request to send the content of an image to a php script on a web server.
The php script (upl.php herebelow) works well when called from an html form using enctype=multipart/form-data.
The script is waiting a variable fileToUpload containing the file name. In a get request, it would be:
http://myserver.org/upl.php?fileToUpload=MyFile.jpg
I can’t come up with a working post request in UE4.
Here is what i am trying:
FString UpFilePath = "/MyPath/MyFile.jpg";
TArray<uint8> UpFileRawData;
FFileHelper::LoadFileToArray(UpFileRawData, *UpFilePath);
TSharedRef<IHttpRequest> FileUploadRequest = (&FHttpModule::Get())->CreateRequest();
FileUploadRequest->SetVerb("POST");
FileUploadRequest->SetHeader("Content-Type", "multipart/form-data; boundary=---------------------------BoNjOuRcOcO");
FileUploadRequest->SetContent(UpFileRawData);
FileUploadRequest->SetHeader("Content-Disposition", "form-data; name=fileToUpload; filename=MyFile.jpg");
FileUploadRequest->SetHeader("Content-Length", FString::FromInt(UpFileRawData.Num()));
FileUploadRequest->OnProcessRequestComplete().BindRaw(this, &SYagOpenFileWidget::HttpUploadYagFileComplete);
FileUploadRequest->SetURL("http://myserver.org/upl.php");
FileUploadRequest->ProcessRequest();
Currently the HttpUploadYagFileComplete delegate merely prints the html string of the response, which displays a “undefined index” error telling me that it can’t find any fileToUpload variable.
How do i correctly specify the fileToUpload pair ?
Also, in the html form, the “multipart/form-data” is set through the “enctype” propertie. How do i correctly specify it in the IHttpRequest request ?
As far as i know, my test hereabove might be filled with errors.
Thanks
Cedric