Skip to content

Commit

Permalink
Added AddStringBody with two overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jan 9, 2022
1 parent c4af0f4 commit 1556eed
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/RestSharp/Request/RestRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,30 @@ public static RestRequest AddFile(this RestRequest request, string name, byte[]
throw new ArgumentException("Non-string body found with unsupported content type", nameof(obj));
}

/// <summary>
/// Adds a string body and figures out the content type from the data format specified. You can, for example, add a JSON string
/// using this method as request body, using DataFormat.Json/>
/// </summary>
/// <param name="request">Request instance</param>
/// <param name="body">String body</param>
/// <param name="dataFormat"><see cref="DataFormat"/> for the content</param>
/// <returns></returns>
public static RestRequest AddStringBody(this RestRequest request, string body, DataFormat dataFormat) {
var contentType = ContentType.FromDataFormat[dataFormat];
request.RequestFormat = dataFormat;
return request.AddParameter(new BodyParameter("", body, contentType));
}

/// <summary>
/// Adds a string body to the request using the specified content type.
/// </summary>
/// <param name="request">Request instance</param>
/// <param name="body">String body</param>
/// <param name="contentType">Content type of the body</param>
/// <returns></returns>
public static RestRequest AddStringBody(this RestRequest request, string body, string contentType)
=> request.AddParameter(new BodyParameter("", body, Ensure.NotEmpty(contentType, nameof(contentType))));

/// <summary>
/// Adds a JSON body parameter to the request
/// </summary>
Expand Down

0 comments on commit 1556eed

Please sign in to comment.