How do i create a post http request for binary file?

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

I found here an answer to my second question:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4

It’s not really UE4 related, but just in case someone reads this, the enctype attribute is a Content-Type header.

It seems that i am correctly using the Content-Disposition header, but it doesn’t work so i must be doing something wrong.

From the same page, i am looking for a way to send this using UE4 functions:

Content-Type: multipart/form-data; boundary=AaB03x

   --AaB03x
   Content-Disposition: form-data; name="submit-name"

   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"; filename="file1.txt"
   Content-Type: text/plain

   ... contents of file1.txt ...
   --AaB03x--

How do i add the boundary between the headers groups ?

How do i use the Content-Disposition header so that my fileToUpload attribute is recognized ?

Hi,

I finally managed to do it, using json. Details and working code here:

Cheers

Cedric