Skip to content

Commit dd40762

Browse files
committed
Web - Multiple frameworks & TextBody
Implement text/plan request body
1 parent 208f724 commit dd40762

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

Web/API/Request.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ public class Request
2424
Formatting = Formatting.Indented
2525
};
2626

27-
/// <summary>
28-
/// Gets the URL to send the request to.
29-
/// </summary>
30-
public string URL { get; private set; } = string.Empty;
27+
/// <summary>
28+
/// Set body as JSON
29+
/// </summary>
30+
private bool _isJsonBody = false;
31+
32+
/// <summary>
33+
/// Gets the URL to send the request to.
34+
/// </summary>
35+
public string URL { get; private set; } = string.Empty;
3136

3237
/// <summary>
3338
/// Gets the HTTP method to use for the request.
@@ -224,8 +229,9 @@ public Request AcceptJson()
224229
public Request AddJsonBody(object body)
225230
{
226231
Body = body;
232+
_isJsonBody = true;
227233

228-
return this;
234+
return this;
229235
}
230236

231237
/// <summary>
@@ -242,6 +248,13 @@ public Request AddByteBody(byte[] document, string? fileName)
242248
return this;
243249
}
244250

251+
public Request AddTextBody(object body)
252+
{
253+
Body = body;
254+
_isJsonBody = false;
255+
return this;
256+
}
257+
245258
/// <summary>
246259
/// Sets the content type of the request.
247260
/// </summary>
@@ -269,9 +282,16 @@ public async Task<Result<T>> Run<T>()
269282

270283
if (Body != null)
271284
{
272-
string json = JsonConvert.SerializeObject(Body, _jsonSerializerSettings);
273-
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
274-
request.Content.Headers.ContentType = new("application/json");
285+
if (_isJsonBody)
286+
{
287+
string json = JsonConvert.SerializeObject(Body, _jsonSerializerSettings);
288+
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
289+
request.Content.Headers.ContentType = new("application/json");
290+
} else
291+
{
292+
request.Content = new StringContent((string)Body, Encoding.UTF8, "text/plain");
293+
request.Content.Headers.ContentType = new("text/plain");
294+
}
275295
}
276296

277297
Result<T> result = await Process<T>(request);

Web/Web.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<AssemblyName>FrApp42.$(MSBuildProjectName)</AssemblyName>
@@ -39,7 +39,7 @@
3939
</ItemGroup>
4040

4141
<ItemGroup>
42-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
42+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
4343
</ItemGroup>
4444

4545
</Project>

0 commit comments

Comments
 (0)