Unitywebrequest using multipart/form-data

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>();
        }

@orehapunnu I would recommend that you use Charles Proxy to compare your working request to the one that is not. We should be able to easily spot the differences. Save both captures in .chls files and post them here for review.

@JeffDUnity3D Thank you for replying my question. I send you the difference of http request body in charles proxy between correct result using curl command and my unitywebrequest code which is attatched again. I predict that the error is caused by the blank on the first line in http request body. Could you tell me how I should revise the code?

 //Upload content for entry

            UploadResponseBody responseBodyJson = JsonUtility.FromJson<UploadResponseBody>(entry.downloadHandler.text);
            //get entryid when create entry API response
            string resopnseBodyEntryid = responseBodyJson.entryid;
            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();
            // Debug.Log($"boundary: {System.Text.Encoding.UTF8.GetString(boundary, 0, boundary.Length)}");
            // 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, boundary);
            content.method = "PATCH";

            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>();
        }

@orehapunnu As .chls files please. You may need to change the extension to txt. But there are clear differences even in your screenshot, like content type

I couldn’t upload .chls file in unity forum and get error “The uploaded file does not have an allowed extension.”. I will send .chls file via unity support &services.

I resolved the problem and fixed wrong code to below correct code.
Thank you.

formData.Add(new MultipartFormFileSection("file", bytes, filename, "application/octet-stream"));