Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,6 @@ internal virtual void FillRequestStream(HttpRequestMessage request)

if (Form is not null)
{
// Content headers will be set by MultipartFormDataContent which will throw unless we clear them first
WebSession.ContentHeaders.Clear();

var formData = new MultipartFormDataContent();
foreach (DictionaryEntry formEntry in Form)
{
Expand All @@ -1099,55 +1096,48 @@ internal virtual void FillRequestStream(HttpRequestMessage request)

SetRequestContent(request, formData);
}
// coerce body into a usable form
else if (Body is not null)
{
// Coerce body into a usable form
object content = Body;

// make sure we're using the base object of the body, not the PSObject wrapper
PSObject psBody = Body as PSObject;
if (psBody is not null)
// Make sure we're using the base object of the body, not the PSObject wrapper
if (Body is PSObject psBody)
{
content = psBody.BaseObject;
}

if (content is FormObject form)
{
SetRequestContent(request, form.Fields);
}
else if (content is IDictionary dictionary && request.Method != HttpMethod.Get)
{
SetRequestContent(request, dictionary);
}
else if (content is XmlNode xmlNode)
{
SetRequestContent(request, xmlNode);
}
else if (content is Stream stream)
{
SetRequestContent(request, stream);
}
else if (content is byte[] bytes)
{
SetRequestContent(request, bytes);
}
else if (content is MultipartFormDataContent multipartFormDataContent)
switch (content)
{
WebSession.ContentHeaders.Clear();
SetRequestContent(request, multipartFormDataContent);
}
else
{
SetRequestContent(
request,
(string)LanguagePrimitives.ConvertTo(content, typeof(string), CultureInfo.InvariantCulture));
case FormObject form:
SetRequestContent(request, form.Fields);
break;
case IDictionary dictionary when request.Method != HttpMethod.Get:
SetRequestContent(request, dictionary);
break;
case XmlNode xmlNode:
SetRequestContent(request, xmlNode);
break;
case Stream stream:
SetRequestContent(request, stream);
break;
case byte[] bytes:
SetRequestContent(request, bytes);
break;
case MultipartFormDataContent multipartFormDataContent:
SetRequestContent(request, multipartFormDataContent);
break;
default:
SetRequestContent(request, (string)LanguagePrimitives.ConvertTo(content, typeof(string), CultureInfo.InvariantCulture));
break;
}
}
else if (InFile is not null) // copy InFile data
else if (InFile is not null)
{
// Copy InFile data
try
{
// open the input file
// Open the input file
SetRequestContent(request, new FileStream(InFile, FileMode.Open, FileAccess.Read, FileShare.Read));
}
catch (UnauthorizedAccessException)
Expand Down Expand Up @@ -1572,44 +1562,32 @@ protected override void ProcessRecord()
/// </summary>
/// <param name="request">The WebRequest who's content is to be set.</param>
/// <param name="content">A byte array containing the content data.</param>
/// <returns>The number of bytes written to the requests RequestStream (and the new value of the request's ContentLength property.</returns>
/// <remarks>
/// Because this function sets the request's ContentLength property and writes content data into the requests's stream,
/// it should be called one time maximum on a given request.
/// </remarks>
internal long SetRequestContent(HttpRequestMessage request, byte[] content)
internal void SetRequestContent(HttpRequestMessage request, byte[] content)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(content);

if (content is null)
{
return 0;
}

var byteArrayContent = new ByteArrayContent(content);
ByteArrayContent byteArrayContent = new(content);
request.Content = byteArrayContent;

return byteArrayContent.Headers.ContentLength.Value;
}

/// <summary>
/// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream.
/// </summary>
/// <param name="request">The WebRequest who's content is to be set.</param>
/// <param name="content">A String object containing the content data.</param>
/// <returns>The number of bytes written to the requests RequestStream (and the new value of the request's ContentLength property.</returns>
/// <remarks>
/// Because this function sets the request's ContentLength property and writes content data into the requests's stream,
/// it should be called one time maximum on a given request.
/// </remarks>
internal long SetRequestContent(HttpRequestMessage request, string content)
internal void SetRequestContent(HttpRequestMessage request, string content)
{
ArgumentNullException.ThrowIfNull(request);

if (content is null)
{
return 0;
}
ArgumentNullException.ThrowIfNull(content);

Encoding encoding = null;
if (ContentType is not null)
Expand All @@ -1629,28 +1607,22 @@ internal long SetRequestContent(HttpRequestMessage request, string content)
{
if (!SkipHeaderValidation)
{
var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex);
ValidationMetadataException outerEx = new(WebCmdletStrings.ContentTypeException, ex);
ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
ThrowTerminatingError(er);
}
}
}

byte[] bytes = StreamHelper.EncodeToBytes(content, encoding);
var byteArrayContent = new ByteArrayContent(bytes);
ByteArrayContent byteArrayContent = new(bytes);
request.Content = byteArrayContent;

return byteArrayContent.Headers.ContentLength.Value;
}

internal long SetRequestContent(HttpRequestMessage request, XmlNode xmlNode)
internal void SetRequestContent(HttpRequestMessage request, XmlNode xmlNode)
{
ArgumentNullException.ThrowIfNull(request);

if (xmlNode is null)
{
return 0;
}
ArgumentNullException.ThrowIfNull(xmlNode);

byte[] bytes = null;
XmlDocument doc = xmlNode as XmlDocument;
Expand All @@ -1665,63 +1637,56 @@ internal long SetRequestContent(HttpRequestMessage request, XmlNode xmlNode)
bytes = StreamHelper.EncodeToBytes(xmlNode.OuterXml, encoding: null);
}

var byteArrayContent = new ByteArrayContent(bytes);
request.Content = byteArrayContent;
ByteArrayContent byteArrayContent = new(bytes);

return byteArrayContent.Headers.ContentLength.Value;
request.Content = byteArrayContent;
}

/// <summary>
/// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream.
/// </summary>
/// <param name="request">The WebRequest who's content is to be set.</param>
/// <param name="contentStream">A Stream object containing the content data.</param>
/// <returns>The number of bytes written to the requests RequestStream (and the new value of the request's ContentLength property.</returns>
/// <remarks>
/// Because this function sets the request's ContentLength property and writes content data into the requests's stream,
/// it should be called one time maximum on a given request.
/// </remarks>
internal long SetRequestContent(HttpRequestMessage request, Stream contentStream)
internal void SetRequestContent(HttpRequestMessage request, Stream contentStream)
{
ArgumentNullException.ThrowIfNull(request);

ArgumentNullException.ThrowIfNull(contentStream);

var streamContent = new StreamContent(contentStream);
StreamContent streamContent = new(contentStream);
request.Content = streamContent;

return streamContent.Headers.ContentLength.Value;
}

/// <summary>
/// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream.
/// </summary>
/// <param name="request">The WebRequest who's content is to be set.</param>
/// <param name="multipartContent">A MultipartFormDataContent object containing multipart/form-data content.</param>
/// <returns>The number of bytes written to the requests RequestStream (and the new value of the request's ContentLength property.</returns>
/// <remarks>
/// Because this function sets the request's ContentLength property and writes content data into the requests's stream,
/// it should be called one time maximum on a given request.
/// </remarks>
internal long SetRequestContent(HttpRequestMessage request, MultipartFormDataContent multipartContent)
internal void SetRequestContent(HttpRequestMessage request, MultipartFormDataContent multipartContent)
{
ArgumentNullException.ThrowIfNull(request);

ArgumentNullException.ThrowIfNull(multipartContent);

// Content headers will be set by MultipartFormDataContent which will throw unless we clear them first
WebSession.ContentHeaders.Clear();

request.Content = multipartContent;

return multipartContent.Headers.ContentLength.Value;
}

internal long SetRequestContent(HttpRequestMessage request, IDictionary content)
internal void SetRequestContent(HttpRequestMessage request, IDictionary content)
{
ArgumentNullException.ThrowIfNull(request);

ArgumentNullException.ThrowIfNull(content);

string body = FormatDictionary(content);
return SetRequestContent(request, body);
SetRequestContent(request, body);
}

internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUri)
Expand Down