Skip to content

Commit fa4b0f1

Browse files
Merge pull request #10 from amingolmahalle/fix/managing-url
Fix/managing url
2 parents 039cd1b + a09595b commit fa4b0f1

File tree

13 files changed

+406
-430
lines changed

13 files changed

+406
-430
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ If you like this project, learn something or you are using it in your applicatio
6262
### **Post Method** sample code (it will be written in the **console**):
6363
```
6464
string requestBody = @"{""name"":""amin"",""requestId"":""10001000"",""amount"":10000}";
65-
string requestUri = "/api/test";
65+
string requestUri = "api/test";
6666
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
6767
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
6868
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
6969
7070
using var httpClient = new HttpClient();
71-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
71+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
7272
7373
httpClient.GenerateCurlInConsole(
7474
httpRequestMessage,
@@ -85,7 +85,7 @@ If you like this project, learn something or you are using it in your applicatio
8585
### **Post Method** sample code for FormUrlEncodedContent (it will be written in the **console**):
8686
```
8787
string requestBody = @"{""name"":""justin"",""requestId"":10001026,""amount"":26000}";
88-
string requestUri = "/api/test";
88+
string requestUri = "api/test";
8989
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
9090
httpRequestMessage.Content = new FormUrlEncodedContent(new[]
9191
{
@@ -95,7 +95,7 @@ If you like this project, learn something or you are using it in your applicatio
9595
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
9696
9797
using var httpClient = new HttpClient();
98-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
98+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
9999
100100
httpClient.GenerateCurlInConsole(
101101
httpRequestMessage,
@@ -120,12 +120,12 @@ If you like this project, learn something or you are using it in your applicatio
120120
<amount>240000</amount>
121121
</Order>";
122122
123-
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "/api/test");
123+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test");
124124
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
125125
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
126126
127127
using var httpClient = new HttpClient();
128-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
128+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
129129
130130
httpClient.GenerateCurlInConsole(
131131
httpRequestMessage,
@@ -148,13 +148,13 @@ If the filename variable is null or empty, then the current date will be set for
148148
string path = string.Empty;
149149
string filename = "PostMethodResult" ;
150150
string requestBody = @"{""name"":""sara"",""requestId"":""10001001"",""amount"":20000}";
151-
string requestUri = "/api/test";
151+
string requestUri = "api/test";
152152
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
153153
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
154154
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
155155
156156
using var httpClient = new HttpClient();
157-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
157+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
158158
159159
httpClient.GenerateCurlInFile(
160160
httpRequestMessage,
@@ -171,13 +171,13 @@ If the filename variable is null or empty, then the current date will be set for
171171

172172
### **Get Method** sample code (it will be written in the **console**):
173173
```
174-
string requestUri = "/api/test";
174+
string requestUri = "api/test";
175175
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
176176
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
177177
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
178178
179179
using var httpClient = new HttpClient();
180-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
180+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
181181
182182
httpClient.GenerateCurlInConsole(
183183
httpRequestMessage,
@@ -199,13 +199,13 @@ If the filename variable is null or empty, then the current date will be set for
199199
```
200200
string path = string.Empty;
201201
string filename = "GetMethodResult";
202-
string requestUri = "/api/test";
202+
string requestUri = "api/test";
203203
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
204204
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
205205
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
206206
207207
using var httpClient = new HttpClient();
208-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
208+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
209209
210210
httpClient.GenerateCurlInFile(
211211
httpRequestMessage,
@@ -223,13 +223,13 @@ If the filename variable is null or empty, then the current date will be set for
223223
### **Put Method** sample code (it will be written in the **console**):
224224
```
225225
string requestBody = @"{""name"":""jadi"",""requestId"":""10001003"",""amount"":30000}";
226-
string requestUri = "/api/test";
226+
string requestUri = "api/test";
227227
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, requestUri);
228228
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
229229
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
230230
231231
using var httpClient = new HttpClient();
232-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
232+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
233233
234234
httpClient.GenerateCurlInConsole(
235235
httpRequestMessage,
@@ -252,13 +252,13 @@ If the filename variable is null or empty, then the current date will be set for
252252
string path = string.Empty;
253253
string filename = "PutMethodResult" ;
254254
string requestBody = @"{ ""name"" : ""reza"",""requestId"" : ""10001004"",""amount"":40000 }";
255-
string requestUri = "/api/test";
255+
string requestUri = "api/test";
256256
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, requestUri);
257257
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
258258
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
259259
260260
using var httpClient = new HttpClient();
261-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
261+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
262262
263263
httpClient.GenerateCurlInFile(
264264
httpRequestMessage,
@@ -276,13 +276,13 @@ If the filename variable is null or empty, then the current date will be set for
276276
### **Patch Method** sample code (it will be written in the **console**):
277277
```
278278
string requestBody = @"{""name"":""hamed"",""requestId"":""10001005"",""amount"":50000}";
279-
string requestUri = "/api/test";
279+
string requestUri = "api/test";
280280
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, requestUri);
281281
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
282282
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
283283
284284
using var httpClient = new HttpClient();
285-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
285+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
286286
287287
httpClient.GenerateCurlInConsole(
288288
httpRequestMessage,
@@ -305,13 +305,13 @@ If the filename variable is null or empty, then the current date will be set for
305305
string path = string.Empty;
306306
string filename = "PatchMethodResult" ;
307307
string requestBody = @"{ ""name"" : ""zara"",""requestId"" : ""10001006"",""amount"":60000 }";
308-
string requestUri = "/api/test";
308+
string requestUri = "api/test";
309309
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, requestUri);
310310
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
311311
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
312312
313313
using var httpClient = new HttpClient();
314-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
314+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
315315
316316
httpClient.GenerateCurlInFile(
317317
httpRequestMessage,
@@ -329,13 +329,13 @@ If the filename variable is null or empty, then the current date will be set for
329329
### **Delete Method** sample code (it will be written in the **console**):
330330
```
331331
int id = 12;
332-
string requestUri = $"/api/test/{id}";
332+
string requestUri = $"api/test/{id}";
333333
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, requestUri);
334334
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
335335
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
336336
337337
using var httpClient = new HttpClient();
338-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
338+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
339339
340340
httpClient.GenerateCurlInConsole(
341341
httpRequestMessage,
@@ -358,13 +358,13 @@ If the filename variable is null or empty, then the current date will be set for
358358
string path = string.Empty;
359359
string filename = "DeleteMethodResult";
360360
int id = 12;
361-
string requestUri = $"/api/test/{id}";
361+
string requestUri = $"api/test/{id}";
362362
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, requestUri);
363363
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
364364
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
365365
366366
using var httpClient = new HttpClient();
367-
httpClient.BaseAddress = new Uri("http://localhost:1213/v1");
367+
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");
368368
369369
httpClient.GenerateCurlInFile(
370370
httpRequestMessage,

src/HttpClientToCurl/Builder.cs

Lines changed: 24 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Net;
22
using System.Text;
3-
using System.Text.Json;
4-
using System.Web;
53
using HttpClientToCurl.Utility;
64

75
namespace HttpClientToCurl;
@@ -24,58 +22,29 @@ internal static StringBuilder Initialize(HttpMethod httpMethod)
2422
return stringBuilder.Append(' ');
2523
}
2624

27-
internal static StringBuilder AddAbsoluteUrl(this StringBuilder stringBuilder, string baseUrl, string requestUri)
25+
internal static StringBuilder AddAbsoluteUrl(this StringBuilder stringBuilder, string inputBaseAddress, Uri inputRequestUri)
2826
{
29-
bool hasSlashEndOfBaseUrl = false;
30-
bool hasSlashFirstOfRequestUri = false;
31-
string splitterUrl = string.Empty;
32-
33-
string inputBaseUrl = baseUrl?.Trim();
34-
if (!string.IsNullOrWhiteSpace(inputBaseUrl))
35-
{
36-
if (inputBaseUrl.EndsWith('/'))
37-
hasSlashEndOfBaseUrl = true;
38-
39-
string inputRequestUri = requestUri?.Trim();
40-
if (!string.IsNullOrWhiteSpace(inputRequestUri))
41-
{
42-
if (inputRequestUri.StartsWith('/'))
43-
hasSlashFirstOfRequestUri = true;
44-
45-
string warningMessage = CheckAndAddWarningMessageForIncorrectSlash(hasSlashEndOfBaseUrl, hasSlashFirstOfRequestUri, out splitterUrl);
46-
if (!string.IsNullOrEmpty(warningMessage))
47-
{
48-
stringBuilder
49-
.Insert(0, warningMessage)
50-
.Insert(warningMessage.Length, Environment.NewLine);
51-
}
52-
}
53-
54-
if (hasSlashEndOfBaseUrl && (string.IsNullOrEmpty(inputRequestUri) || hasSlashFirstOfRequestUri))
55-
inputBaseUrl = inputBaseUrl.Remove(inputBaseUrl.Length - 1);
56-
57-
return stringBuilder
58-
.Append($"{inputBaseUrl}{splitterUrl}{inputRequestUri}")
59-
.Append(' ');
60-
}
61-
62-
throw new InvalidDataException("baseUrl argument is null or empty!");
63-
64-
string CheckAndAddWarningMessageForIncorrectSlash(bool hasOnBaseUrl, bool hasOnRequestUri, out string splitter)
65-
{
66-
string message = string.Empty;
67-
splitter = string.Empty;
68-
69-
if (hasOnBaseUrl && hasOnRequestUri)
70-
message = "# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.";
71-
else if (!hasOnBaseUrl && !hasOnRequestUri)
72-
{
73-
splitter = "/";
74-
message = "# Warning: you must add the Slash at the end of base url or at the first of the requestUri.";
75-
}
27+
string requestUri;
28+
Uri baseAddressUri = Helpers.CreateUri(inputBaseAddress);
29+
bool baseAddressIsAbsoluteUri = Helpers.CheckAddressIsAbsoluteUri(baseAddressUri);
30+
bool requestUriIsAbsoluteUri = Helpers.CheckAddressIsAbsoluteUri(inputRequestUri);
31+
32+
if (inputRequestUri is null && baseAddressUri is not null && baseAddressIsAbsoluteUri)
33+
requestUri = baseAddressUri.ToString();
34+
else if (baseAddressUri is null && inputRequestUri is not null && requestUriIsAbsoluteUri)
35+
requestUri = inputRequestUri.ToString();
36+
else if (baseAddressUri is not null && inputRequestUri is not null && baseAddressIsAbsoluteUri && !requestUriIsAbsoluteUri)
37+
requestUri = new Uri(baseAddressUri, inputRequestUri).ToString();
38+
else if (baseAddressUri is not null && inputRequestUri is not null && baseAddressIsAbsoluteUri)
39+
requestUri = inputRequestUri.ToString();
40+
else if (baseAddressUri is null && inputRequestUri is null)
41+
requestUri = null;
42+
else
43+
requestUri = $"{baseAddressUri}{inputRequestUri}";
7644

77-
return message;
78-
}
45+
return stringBuilder
46+
.Append($"{requestUri}")
47+
.Append(' ');
7948
}
8049

8150
internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpClient httpClient, HttpRequestMessage httpRequestMessage, bool needAddDefaultHeaders = true)
@@ -112,7 +81,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
11281
hasHeader = true;
11382
}
11483

115-
if (httpRequestMessage.Content != null && httpRequestMessage.Content.Headers.Any())
84+
if (httpRequestMessage.Content is not null && httpRequestMessage.Content.Headers.Any())
11685
{
11786
foreach (var header in httpRequestMessage.Content.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString()))
11887
{
@@ -137,36 +106,11 @@ internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpCont
137106
string contentType = content?.Headers?.ContentType?.MediaType;
138107
string body = content?.ReadAsStringAsync().GetAwaiter().GetResult();
139108

140-
if (content is not null && !string.IsNullOrWhiteSpace(body) && !Helpers.IsValidBody(body, contentType))
141-
throw new JsonException($"exception in parsing request body {contentType}!{Environment.NewLine}request body:{Environment.NewLine}{body}");
142-
143109
if (contentType == "application/x-www-form-urlencoded")
144-
_AddFormUrlEncodedContentBody(stringBuilder, body);
110+
stringBuilder.AddFormUrlEncodedContentBody(body);
145111
else
146-
_AppendBodyItem(stringBuilder, body);
112+
stringBuilder.AppendBodyItem(body);
147113

148114
return stringBuilder;
149115
}
150-
151-
private static void _AddFormUrlEncodedContentBody(StringBuilder stringBuilder, string body)
152-
{
153-
string decodedBody = HttpUtility.UrlDecode(body);
154-
string[] splitBodyArray = decodedBody.Split('&');
155-
if (splitBodyArray.Any())
156-
{
157-
foreach (string item in splitBodyArray)
158-
{
159-
_AppendBodyItem(stringBuilder, item);
160-
}
161-
}
162-
}
163-
164-
private static void _AppendBodyItem(StringBuilder stringBuilder, object body)
165-
=> stringBuilder
166-
.Append("-d")
167-
.Append(' ')
168-
.Append('\'')
169-
.Append(body)
170-
.Append('\'')
171-
.Append(' ');
172116
}

0 commit comments

Comments
 (0)