diff --git a/Runtime/Param/FormParam.cs b/Runtime/Param/FormParam.cs index d5cf894..bedd554 100644 --- a/Runtime/Param/FormParam.cs +++ b/Runtime/Param/FormParam.cs @@ -1,25 +1,31 @@ +using System.Text; using UnityEngine.Networking; namespace PinealCtx.RestClient.Param { public class FormParam : IParam { - public FormParam(IMultipartFormSection section) => Section = section; - - public FormParam(string name, string value) : this(new MultipartFormDataSection(name, value)) - { - } - - public FormParam(string name, byte[] value, string fileName = "", string contentType = "") : this( - new MultipartFormFileSection(name, value, fileName, contentType)) + public FormParam(string fieldName, string value, Encoding encoding = null) { + FieldName = fieldName; + Value = value; + Encoding = encoding ?? Encoding.UTF8; } - public FormParam(string name, FormFile file) : this(name, file.Data, file.FileName, file.ContentType) + public FormParam(string fieldName, byte[] data, string fileName = null, string mimeType = null) { + FieldName = fieldName; + Data = data; + FileName = fileName; + MimeType = mimeType; } - public IMultipartFormSection Section { get; set; } + public string FieldName { get; } + public string Value { get; } + public Encoding Encoding { get; } + public byte[] Data { get; } + public string FileName { get; } + public string MimeType { get; } public ParamType ParamType() => Param.ParamType.Form; } @@ -28,6 +34,5 @@ public class FormFile { public byte[] Data { get; set; } public string FileName { get; set; } - public string ContentType { get; set; } } } \ No newline at end of file diff --git a/Runtime/RestRequest.cs b/Runtime/RestRequest.cs index d673c48..4bc2722 100644 --- a/Runtime/RestRequest.cs +++ b/Runtime/RestRequest.cs @@ -5,7 +5,9 @@ using Newtonsoft.Json; using PinealCtx.RestClient.Attribute; using PinealCtx.RestClient.Param; +using UnityEngine; using UnityEngine.Networking; +using RestHeaderProperty = PinealCtx.RestClient.Attribute.RestHeaderProperty; namespace PinealCtx.RestClient { @@ -29,7 +31,7 @@ public RestRequest(string path) : this(Method.Get, path) public List Headers { get; set; } = new(); - public List Forms { get; set; } = new(); + public WWWForm Forms { get; set; } public BodyParam Body { get; set; } @@ -51,7 +53,15 @@ public RestRequest WithParam(IParam param) Body = bodyParam; break; case FormParam formParam: - Forms.Add(formParam); + if (formParam.Data == null) + { + Forms.AddField(formParam.FieldName, formParam.Value, formParam.Encoding); + } + else + { + Forms.AddBinaryData(formParam.FieldName, formParam.Data, formParam.MimeType); + } + break; } @@ -80,24 +90,6 @@ public RestRequest WithHeader(string name, string value) return this; } - public RestRequest WithForm(string name, string value) - { - Forms.Add(new FormParam(name, value)); - return this; - } - - public RestRequest WithForm(string name, FormFile file) - { - Forms.Add(new FormParam(name, file)); - return this; - } - - public RestRequest WithForm(string name, byte[] value) - { - Forms.Add(new FormParam(name, value)); - return this; - } - public RestRequest WithBody(string contentType, byte[] body) { Body = new BodyParam(contentType, body); @@ -137,16 +129,17 @@ public RestRequest WithParamObject(object obj) case RestFormProperty formProperty: { hasForm = true; + Forms ??= new WWWForm(); var value = prop.GetValue(obj); - if (value.GetType() == typeof(FormFile)) + if (value == null) continue; + if (value.GetType() != typeof(FormFile)) { - WithForm(formProperty.Name, value as FormFile); - } - else - { - WithForm(formProperty.Name, Object2String(value)); + Forms.AddField(formProperty.Name, Object2String(value)); + continue; } + if (value is FormFile formFile) Forms.AddBinaryData(formProperty.Name, formFile.Data); + break; } } @@ -219,13 +212,10 @@ public UploadHandlerRaw BuildBody() if (Method == Method.Get) return null; if (Body == null) { - if (Forms.Count == 0) return null; - var sections = Forms.Select(f => f.Section).ToList(); - var boundary = UnityWebRequest.GenerateBoundary(); - var data = UnityWebRequest.SerializeFormSections(sections, boundary); - if (data == null) return null; - var contentType = - $"multipart/form-data; boundary={Encoding.UTF8.GetString(boundary, 0, boundary.Length)}"; + if (Forms == null) return null; + var data = Forms.data; + if (data.Length == 0) return null; + Forms.headers.TryGetValue("Content-Type", out var contentType); Body = new BodyParam(contentType, data); } @@ -239,7 +229,7 @@ public void Dispose() Queries?.Clear(); Queries = null; Headers?.Clear(); - Forms?.Clear(); + Forms = null; Headers = null; Body = null; }