forked from haga-rak/fluxzy.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (54 loc) · 2.44 KB
/
Copy pathProgram.cs
File metadata and controls
70 lines (54 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Fluxzy;
using Fluxzy.Rules.Actions;
using Fluxzy.Rules.Filters.RequestFilters;
using System.Net;
using System.Text;
namespace Samples.No017.TransformRequestBody
{
internal class Program
{
/// <summary>
/// This example shows how to use request body transformation.
/// Request body transformation allows you to modify the request body according to the original request body content.
/// </summary>
/// <param name="args"></param>
static async Task Main(string[] args)
{
// Create a default run settings
var fluxzyStartupSetting = FluxzySetting.CreateLocalRandomPort();
// When a json request is received, the body will be transformed to uppercase.
fluxzyStartupSetting.ConfigureRule()
.WhenAny(new JsonRequestFilter()) // take json only
.Do(new TransformRequestBodyAction(async (transformContext, bodyReader) => {
var content = await bodyReader.ConsumeAsString();
// Use bodyReader.ConsumeAsStream() to avoid reading the body into memory
return content.ToUpperInvariant();
}));
// Create a proxy instance
await using var proxy = new Proxy(fluxzyStartupSetting);
var endpoints = proxy.Run();
using var httpClient = new HttpClient(new HttpClientHandler()
{
Proxy = new WebProxy($"http://127.0.0.1:{endpoints.First().Port}"),
UseProxy = true
});
// Make a request to the httpbin service /anything
using var response = await httpClient.PostAsync("https://httpbin.org/anything"
, new StringContent("{\"hello\":\"world\"}", Encoding.UTF8, "application/json"));
var contentString = await response.Content.ReadAsStringAsync();
var httpBinResponse = System.Text.Json.JsonSerializer.Deserialize<HttpBinResponse>(contentString)!;
var result = httpBinResponse.Data;
Console.WriteLine(result);
Console.ReadKey();
}
}
internal class HttpBinResponse
{
public HttpBinResponse(string data)
{
Data = data;
}
[System.Text.Json.Serialization.JsonPropertyName("data")]
public string Data { get; }
}
}