I try to upload content for entry in Cloud Content Delivry System using unitywebrequest, but I fail to upload content and get alert “HTTP/1.1 400 Bad Request”. The cause may be that the setting of multipart/form-data of unitywebrequest is wrong.
(I have already successd “upload content for entry” via curl command and made sure the unitywebrequest code without multipart/form-data part is correct)
Could you teach which code below is wrong? (Extracted only the code related to multipart / form-data prat)
//Upload content for entry
UploadResponseBody responseBodyJson = JsonUtility.FromJson<UploadResponseBody>(entry.downloadHandler.text);
//get entryid when create entry API response
string resopnseBodyEntryid = responseBodyJson.entryid;
//make request body " "file":{} "
UploadContentRequestBody uploadContentRequestBody = new UploadContentRequestBody();
string contentRequestBodyJson = Newtonsoft.Json.JsonConvert.SerializeObject(uploadContentRequestBody);
byte[] contentBody = Encoding.UTF8.GetBytes(contentRequestBodyJson);
//multipart/dorm-data set
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
var bytes = File.ReadAllBytes(datapath);
string filename = Path.GetFileName(datapath);
formData.Add(new MultipartFormFileSection(filename, bytes ));
byte[] boundary = UnityWebRequest.GenerateBoundary();
byte[] formSections = UnityWebRequest.SerializeFormSections(formData, boundary);
string contentpaths = string.Format("https://content-api.cloud.unity3d.com/api/v1/environments/{0}/buckets/{1}/entries/{2}/content/", _cCDParameterConfig.Environments_ID, _cCDParameterConfig.Brucket_ID, resopnseBodyEntryid);
UnityWebRequest content = UnityWebRequest.Post(contentpaths, formData, formSections);
content.method = "PATCH";
content.uploadHandler = new UploadHandlerRaw(contentBody);
content.uploadHandler.contentType = "multipart/form-data; boundary=" + System.Text.Encoding.UTF8.GetString(boundary, 0, boundary.Length);
content.downloadHandler = new DownloadHandlerBuffer();
content.SetRequestHeader("AUTHORIZATION", authorization);
content.SetRequestHeader("Content-Type", "multipart/form-data");
await content.SendWebRequest();
if (content.isNetworkError || content.isHttpError)
{
Debug.Log(content.error);
}
else
{
if (content.responseCode == 200)
{
Debug.Log("content success");
Debug.Log(content.downloadHandler.text);
}
else
{
Debug.Log("content failed");
Debug.Log(content.responseCode);
Debug.Log(content.downloadHandler.text);
}
}
}
string authenticate(string username, string password)
{
string auth = username + ":" + password;
auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
auth = "Basic " + auth;
return auth;
}
[Serializable]
public class UploadContentRequestBody
{
public Dictionary<string, string> file = new Dictionary<string, string>();
}