Tuesday, 31 May 2016

Post FormData with Multiple Params

 
 public class FormDataBuilder
    {
        private readonly List _params = new List();

        private readonly string _boundary;

        private const string BoundaryDashes = "--";

        public FormDataBuilder(string boundary)
        {
            _boundary = boundary;
        }

        public string Build()
        {
            StringBuilder sb = new StringBuilder();
            
            foreach (FormDataParam param in _params)
            {
                sb.AppendLine(BoundaryDashes + _boundary);

                sb.AppendLine(param.ParamType == FormDataParamType.File
                    ? string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", param.Name, param.FileName)
                    : string.Format("Content-Disposition: form-data; name=\"{0}\"", param.Name));

                sb.AppendLine("Content-Type: {0}".FormatWith(param.ContentType.Description()));
                sb.AppendLine();
                sb.AppendLine(param.Value);
            }

            sb.AppendLine(BoundaryDashes + _boundary + BoundaryDashes);

            return sb.ToString(); 
        }

        public FormDataBuilder AddImage(string name, string value, string fileName)
        {
            return Add(FormDataParam.CreateImage(name, value, fileName));
        }

        public FormDataBuilder AddAudio(string name, string value, string fileName)
        {
            return Add(FormDataParam.CreateAudio(name, value, fileName));
        }

        public FormDataBuilder AddText(string name, string value)
        {
            return Add(FormDataParam.CreateField(name, value, ContentType.Text));
        }

        public FormDataBuilder AddJson(string name, string value)
        {
            return Add(FormDataParam.CreateField(name, value, ContentType.Json));
        }

        public FormDataBuilder Add(FormDataParam formDataParam)
        {
            _params.Add(formDataParam);

            return this;
        }
    }

 public enum FormDataParamType
    {
        Field,
        File
    }

    public enum ContentType
    {
        [Description("text/plain")]
        Text,
        [Description("application/json")]
        Json,
        [Description("image/jpg")]
        Image,
        [Description("audio/wav")]
        Audio
    }

    public class FormDataParam
    {
        public string Name;
        public string FileName;
        public string Value;
        public FormDataParamType ParamType;
        public ContentType ContentType;

        public static FormDataParam CreateField(string name, string value, ContentType contentType)
        {
            return new FormDataParam()
            {
                Name = name,
                Value = value,
                ParamType = FormDataParamType.Field,
                ContentType = contentType
            };
        }

        public static FormDataParam CreateImage(string name, string value, string fileName)
        {
            return new FormDataParam()
            {
                Name = name,
                Value = value,
                ParamType = FormDataParamType.File,
                FileName = fileName,
                ContentType = ContentType.Image
            };
        }

        public static FormDataParam CreateAudio(string name, string value, string fileName)
        {
            return new FormDataParam()
            {
                Name = name,
                Value = value,
                ParamType = FormDataParamType.File,
                FileName = fileName,
                ContentType = ContentType.Audio
            };
        }
    }

No comments: