Skip to content

Commit 1556eed

Browse files
committed
Added AddStringBody with two overloads
1 parent c4af0f4 commit 1556eed

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/RestSharp/Request/RestRequestExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,30 @@ public static RestRequest AddBody(this RestRequest request, object obj, string?
317317
throw new ArgumentException("Non-string body found with unsupported content type", nameof(obj));
318318
}
319319

320+
/// <summary>
321+
/// Adds a string body and figures out the content type from the data format specified. You can, for example, add a JSON string
322+
/// using this method as request body, using DataFormat.Json/>
323+
/// </summary>
324+
/// <param name="request">Request instance</param>
325+
/// <param name="body">String body</param>
326+
/// <param name="dataFormat"><see cref="DataFormat"/> for the content</param>
327+
/// <returns></returns>
328+
public static RestRequest AddStringBody(this RestRequest request, string body, DataFormat dataFormat) {
329+
var contentType = ContentType.FromDataFormat[dataFormat];
330+
request.RequestFormat = dataFormat;
331+
return request.AddParameter(new BodyParameter("", body, contentType));
332+
}
333+
334+
/// <summary>
335+
/// Adds a string body to the request using the specified content type.
336+
/// </summary>
337+
/// <param name="request">Request instance</param>
338+
/// <param name="body">String body</param>
339+
/// <param name="contentType">Content type of the body</param>
340+
/// <returns></returns>
341+
public static RestRequest AddStringBody(this RestRequest request, string body, string contentType)
342+
=> request.AddParameter(new BodyParameter("", body, Ensure.NotEmpty(contentType, nameof(contentType))));
343+
320344
/// <summary>
321345
/// Adds a JSON body parameter to the request
322346
/// </summary>

0 commit comments

Comments
 (0)