First of all the first parameter of UnityWebRequest.Put
is not your tex
where the ToString
implicitly returns tex.name
but rather the URI!
And then note that curl -F
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC2388. This enables uploading of binary files etc
This anyway uses POST, not PUT.
And in a UnityWebRequest.Post
you can easily add form parts using e.g. a WWWForm
and AddField
and AddBinaryData
like e.g.
public void UploadTexture(Texture2D tex)
{
StartCoroutine(UploadTextureRoutine(tex));
}
private IEnumerator UploadTextureRoutine(Texture2D tex)
{
var bytes = tex.EncodeToPNG();
var form = new WWWForm();
form.AddField("id", "Image01");
form.AddBinaryData("image", bytes, $"{tex.name}.png", "image/png");
using(var unityWebRequest = UnityWebRequest.Post("https://mywebsite.com", form))
{
unityWebRequest.SetRequestHeader("Authorization", "Token 555myToken555");
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.result != UnityWebRequest.Result.Success)
{
print($"Failed to upload {tex.name}: {unityWebRequest.result} - {unityWebRequest.error}");
}
else
{
print($"Finished Uploading {tex.name}");
}
}
}
Also is Put() better in performance than Post()
They are Completely different Protocols.