Skip to content

Commit 33446e7

Browse files
committed
Add C# HttpClient target
This target uses the default HttpClient class which doesn't require any additional NuGet Packages.
1 parent cc8e368 commit 33446e7

19 files changed

+390
-1
lines changed

src/targets/csharp/httpclient.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'use strict'
2+
3+
var CodeBuilder = require('../../helpers/code-builder')
4+
5+
module.exports = function (source, options) {
6+
var indentation = ' '
7+
var code = new CodeBuilder(indentation)
8+
9+
var clienthandler = ''
10+
if (source.allHeaders.cookie) {
11+
clienthandler = 'new HttpClientHandler { UseCookies = false }'
12+
}
13+
14+
code.push('var client = new HttpClient(%s);', clienthandler)
15+
code.push('var request = new HttpRequestMessage')
16+
code.push('{')
17+
18+
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE' ]
19+
var method = source.method.toUpperCase()
20+
if (method && (methods.indexOf(method) !== -1)) {
21+
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}`
22+
} else {
23+
method = `new HttpMethod("${method}")`
24+
}
25+
code.push(1, 'Method = %s,', method)
26+
27+
code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl)
28+
29+
var headers = Object.keys(source.allHeaders).filter(function (header) {
30+
return header !== 'content-type'
31+
})
32+
if (headers.length) {
33+
code.push(1, 'Headers =')
34+
code.push(1, '{')
35+
headers.forEach(function (key) {
36+
code.push(2, '{ "%s", "%s" },', key, source.allHeaders[key])
37+
})
38+
code.push(1, '},')
39+
}
40+
41+
if (source.postData.text) {
42+
const contentType = source.postData.mimeType
43+
switch (contentType) {
44+
case 'application/x-www-form-urlencoded':
45+
code.push(1, 'Content = new FormUrlEncodedContent(new Dictionary<string, string>')
46+
code.push(1, '{')
47+
source.postData.params.forEach(function (param) {
48+
code.push(2, '{ "%s", "%s" },', param.name, param.value)
49+
})
50+
code.push(1, '}),')
51+
break
52+
case 'multipart/form-data':
53+
code.push(1, 'Content = new MultipartFormDataContent')
54+
code.push(1, '{')
55+
source.postData.params.forEach(function (param) {
56+
code.push(2, 'new StringContent(%s)', JSON.stringify(param.value || ''))
57+
code.push(2, '{')
58+
code.push(3, 'Headers =')
59+
code.push(3, '{')
60+
if (param.contentType) {
61+
code.push(4, 'ContentType = new MediaTypeHeaderValue("%s"),', param.contentType)
62+
}
63+
code.push(4, 'ContentDisposition = new ContentDispositionHeaderValue("form-data")')
64+
code.push(4, '{')
65+
code.push(5, 'Name = "%s",', param.name)
66+
if (param.fileName) {
67+
code.push(5, 'FileName = "%s",', param.fileName)
68+
}
69+
code.push(4, '}')
70+
code.push(3, '}')
71+
code.push(2, '},')
72+
})
73+
74+
code.push(1, '},')
75+
break
76+
default:
77+
code.push(1, 'Content = new StringContent(%s)', JSON.stringify(source.postData.text || ''))
78+
code.push(1, '{')
79+
code.push(2, 'Headers =')
80+
code.push(2, '{')
81+
code.push(3, 'ContentType = new MediaTypeHeaderValue("%s")', contentType)
82+
code.push(2, '}')
83+
code.push(1, '}')
84+
break
85+
}
86+
}
87+
88+
code.push('};')
89+
code.push('using (var response = await client.SendAsync(request))')
90+
code.push('{')
91+
code.push(1, 'response.EnsureSuccessStatusCode();')
92+
code.push(1, 'var body = await response.Content.ReadAsStringAsync();')
93+
code.push(1, 'Console.WriteLine(body);')
94+
code.push('}')
95+
return code.join()
96+
}
97+
98+
module.exports.info = {
99+
key: 'httpclient',
100+
title: 'HttpClient',
101+
link: 'https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient',
102+
description: '.NET Standard HTTP Client'
103+
}

src/targets/csharp/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = {
88
default: 'restsharp'
99
},
1010

11-
restsharp: require('./restsharp')
11+
restsharp: require('./restsharp'),
12+
httpclient: require('./httpclient')
1213
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@
230230
"title": "RestSharp",
231231
"link": "http://restsharp.org/",
232232
"description": "Simple REST and HTTP API Client for .NET"
233+
},
234+
{
235+
"key": "httpclient",
236+
"title": "HttpClient",
237+
"link": "https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient",
238+
"description": ".NET Standard HTTP Client"
233239
}
234240
]
235241
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Post,
5+
RequestUri = new Uri("http://mockbin.com/har"),
6+
Content = new FormUrlEncodedContent(new Dictionary<string, string>
7+
{
8+
{ "foo", "bar" },
9+
{ "hello", "world" },
10+
}),
11+
};
12+
using (var response = await client.SendAsync(request))
13+
{
14+
response.EnsureSuccessStatusCode();
15+
var body = await response.Content.ReadAsStringAsync();
16+
Console.WriteLine(body);
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Post,
5+
RequestUri = new Uri("http://mockbin.com/har"),
6+
Content = new StringContent("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
7+
{
8+
Headers =
9+
{
10+
ContentType = new MediaTypeHeaderValue("application/json")
11+
}
12+
}
13+
};
14+
using (var response = await client.SendAsync(request))
15+
{
16+
response.EnsureSuccessStatusCode();
17+
var body = await response.Content.ReadAsStringAsync();
18+
Console.WriteLine(body);
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Post,
5+
RequestUri = new Uri("http://mockbin.com/har"),
6+
Headers =
7+
{
8+
{ "cookie", "foo=bar; bar=baz" },
9+
},
10+
};
11+
using (var response = await client.SendAsync(request))
12+
{
13+
response.EnsureSuccessStatusCode();
14+
var body = await response.Content.ReadAsStringAsync();
15+
Console.WriteLine(body);
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage
3+
{
4+
Method = new HttpMethod("PROPFIND"),
5+
RequestUri = new Uri("http://mockbin.com/har"),
6+
};
7+
using (var response = await client.SendAsync(request))
8+
{
9+
response.EnsureSuccessStatusCode();
10+
var body = await response.Content.ReadAsStringAsync();
11+
Console.WriteLine(body);
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Post,
5+
RequestUri = new Uri("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"),
6+
Headers =
7+
{
8+
{ "cookie", "foo=bar; bar=baz" },
9+
{ "accept", "application/json" },
10+
},
11+
Content = new FormUrlEncodedContent(new Dictionary<string, string>
12+
{
13+
{ "foo", "bar" },
14+
}),
15+
};
16+
using (var response = await client.SendAsync(request))
17+
{
18+
response.EnsureSuccessStatusCode();
19+
var body = await response.Content.ReadAsStringAsync();
20+
Console.WriteLine(body);
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Get,
5+
RequestUri = new Uri("http://mockbin.com/har"),
6+
Headers =
7+
{
8+
{ "accept", "application/json" },
9+
{ "x-foo", "Bar" },
10+
},
11+
};
12+
using (var response = await client.SendAsync(request))
13+
{
14+
response.EnsureSuccessStatusCode();
15+
var body = await response.Content.ReadAsStringAsync();
16+
Console.WriteLine(body);
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var client = new HttpClient();
2+
var request = new HttpRequestMessage
3+
{
4+
Method = HttpMethod.Get,
5+
RequestUri = new Uri("https://mockbin.com/har"),
6+
};
7+
using (var response = await client.SendAsync(request))
8+
{
9+
response.EnsureSuccessStatusCode();
10+
var body = await response.Content.ReadAsStringAsync();
11+
Console.WriteLine(body);
12+
}

0 commit comments

Comments
 (0)