@@ -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 ) ;
0 commit comments