[Unity API] Why doesn't UnityWebRequest support byte array post data?

We know that WWW has a contructor with a byte[ ] parameter as the post data, and it’s quite handy to deal with encoding senstive servers. Why doesn’t UnityWebRequest.Post has a similar overload? I know the string post data version but it’ll cause our Django server to complain.

I think you can do something like this:

byte[] ByteArray;

UnityWebRequest WebRequest = new UnityWebRequest( URL , UnityWebRequest.kHttpVerbPOST );
UploadHandlerRaw MyUploadHandler = new UploadHandlerRaw( ByteArray );
MyUploadHandler.contentType= "application/x-www-form-urlencoded"; // might work with 'multipart/form-data'
WebRequest.uploadHandler= MyUploadHandler;
2 Likes

Thanks for your answer. Yup, we can use this, but this doesn’t smell good.

System.Text has string encoders built-in:

UnicodeEncoding.Unicode.GetString(bytearray)

Same for ASCII, UTF8, UTF32, etc. …

Set up the handler as a PUT, apply the message body, change the method to a POST and Unity won’t auto-mangle the content.

Thanks. It seems WWWForm instead of WWWForm.data work for us.

Actualy you can!

using (UnityWebRequest www = new UnityWebRequest(markerURL))
{
yield return www;

byte[ ] yourBytes = www.downloadHandler.data;

1 Like

You can.

string pathToImage = null;

using (UnityWebRequest www = new UnityWebRequest(markerURL))
{
yield return www;

byte[ ] yourBytes = www.downloadHandler.data;

pathToImageC = Application.persistentDataPath + “/” + f_type;

if (!Directory.Exists(Path.GetDirectoryName(pathToImageC)))
Directory.CreateDirectory(Path.GetDirectoryName(pathToImageC));
else
{
Directory.Delete(pathToImageC);
Directory.CreateDirectory(Path.GetDirectoryName(pathToImageC));
}
System.IO.File.WriteAllBytes(pathToImageC, yourBytes);
}