Skip to content

Commit

Permalink
Restore AlwaysMultipart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jul 4, 2023
1 parent 2b032e2 commit 6d9cf85
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions src/RestSharp/Request/RequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,45 @@ public RequestContent(RestClient client, RestRequest request) {
_parameters = new RequestParameters(_request.Parameters.Union(_client.DefaultParameters));
}

public HttpContent BuildContent()
{
var postParameters = _parameters.GetContentParameters(_request.Method).ToArray();
public HttpContent BuildContent() {
var postParameters = _parameters.GetContentParameters(_request.Method).ToArray();
var postParametersExists = postParameters.Length > 0;
var bodyParametersExists = _request.TryGetBodyParameter(out var bodyParameter);
var filesExists = _request.Files.Any();
var filesExists = _request.Files.Any();

if (postParametersExists ||
_request.HasFiles() ||
BodyShouldBeMultipartForm(bodyParameter) ||
filesExists ||
_request.AlwaysMultipartFormData) {
Content = CreateMultipartFormDataContent();
}

if (filesExists)
AddFiles();
if (filesExists) AddFiles();

if (bodyParametersExists)
AddBody(postParametersExists, bodyParameter!);
if (bodyParametersExists) AddBody(postParametersExists, bodyParameter!);

if (postParametersExists)
AddPostParameters(postParameters);
if (postParametersExists) AddPostParameters(postParameters);

AddHeaders();

return Content!;
}

void AddFiles()
{
// File uploading without multipart/form-data
if (_request.AlwaysSingleFileAsContent && _request.Files.Count == 1)
{
var fileParameter = _request.Files.First();
Content = ToStreamContent(fileParameter);
return;
}

var mpContent = new MultipartFormDataContent(GetOrSetFormBoundary());

foreach (var fileParameter in _request.Files)
mpContent.Add(ToStreamContent(fileParameter));

Content = mpContent;
}

StreamContent ToStreamContent(FileParameter fileParameter)
{

void AddFiles() {
// File uploading without multipart/form-data
if (_request is { AlwaysSingleFileAsContent: true, Files.Count: 1 }) {
var fileParameter = _request.Files.First();
Content?.Dispose();
Content = ToStreamContent(fileParameter);
return;
}

var mpContent = Content as MultipartFormDataContent;
foreach (var fileParameter in _request.Files) mpContent!.Add(ToStreamContent(fileParameter));
}

StreamContent ToStreamContent(FileParameter fileParameter) {
var stream = fileParameter.GetFile();
_streams.Add(stream);
var streamContent = new StreamContent(stream);
Expand Down Expand Up @@ -123,7 +121,9 @@ HttpContent GetSerialized() {
}
}

static bool BodyShouldBeMultipartForm(BodyParameter bodyParameter) {
static bool BodyShouldBeMultipartForm(BodyParameter? bodyParameter) {
if (bodyParameter == null) return false;

var bodyContentType = bodyParameter.ContentType.OrValue(bodyParameter.Name);
return bodyParameter.Name.IsNotEmpty() && bodyParameter.Name != bodyContentType;
}
Expand All @@ -139,8 +139,7 @@ MultipartFormDataContent CreateMultipartFormDataContent() {
return mpContent;
}

void AddBody(bool hasPostParameters, BodyParameter bodyParameter)
{
void AddBody(bool hasPostParameters, BodyParameter bodyParameter) {
var bodyContent = Serialize(bodyParameter);

// we need to send the body
Expand Down

0 comments on commit 6d9cf85

Please sign in to comment.