From 4102d4474eeb84c1a2603633206f8bc59f05abe5 Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 25 Sep 2018 09:42:13 +0200 Subject: [PATCH] Added requestBodyFormat --- CryptoExchange.Net/ExchangeClient.cs | 26 ++++++++++++++++++++------ CryptoExchange.Net/Objects/Enums.cs | 6 ++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CryptoExchange.Net/ExchangeClient.cs b/CryptoExchange.Net/ExchangeClient.cs index b4f3d19..1bcd3e4 100644 --- a/CryptoExchange.Net/ExchangeClient.cs +++ b/CryptoExchange.Net/ExchangeClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; @@ -8,6 +9,7 @@ using System.Net.Sockets; using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Web; using CryptoExchange.Net.Attributes; using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Interfaces; @@ -30,7 +32,8 @@ namespace CryptoExchange.Net protected RateLimitingBehaviour rateLimitBehaviour; protected PostParameters postParametersPosition = PostParameters.InBody; - + protected RequestBodyFormat requestBodyFormat = RequestBodyFormat.Json; + protected AuthenticationProvider authProvider; private List rateLimiters; @@ -186,7 +189,7 @@ namespace CryptoExchange.Net uriString += parameters.CreateParamString(); var request = RequestFactory.Create(uriString); - request.ContentType = "application/json"; + request.ContentType = requestBodyFormat == RequestBodyFormat.Json ? "application/json": "application/x-www-form-urlencoded"; request.Accept = "application/json"; request.Method = method; @@ -208,7 +211,7 @@ namespace CryptoExchange.Net return request; } - protected void WriteParamBody(IRequest request, string stringData) + protected virtual void WriteParamBody(IRequest request, string stringData) { var data = Encoding.UTF8.GetBytes(stringData); request.ContentLength = data.Length; @@ -217,10 +220,21 @@ namespace CryptoExchange.Net stream.Write(data, 0, data.Length); } - protected void WriteParamBody(IRequest request, Dictionary parameters) + protected virtual void WriteParamBody(IRequest request, Dictionary parameters) { - var stringData = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); - WriteParamBody(request, stringData); + if (requestBodyFormat == RequestBodyFormat.Json) + { + var stringData = JsonConvert.SerializeObject(parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value)); + WriteParamBody(request, stringData); + } + else if(requestBodyFormat == RequestBodyFormat.FormData) + { + NameValueCollection formData = HttpUtility.ParseQueryString(String.Empty); + foreach (var kvp in parameters.OrderBy(p => p.Key)) + formData.Add(kvp.Key, kvp.Value.ToString()); + string stringData = formData.ToString(); + WriteParamBody(request, stringData); + } } private async Task> ExecuteRequest(IRequest request) diff --git a/CryptoExchange.Net/Objects/Enums.cs b/CryptoExchange.Net/Objects/Enums.cs index ccf0287..7e821d8 100644 --- a/CryptoExchange.Net/Objects/Enums.cs +++ b/CryptoExchange.Net/Objects/Enums.cs @@ -11,4 +11,10 @@ InBody, InUri } + + public enum RequestBodyFormat + { + FormData, + Json + } }