Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.Graph.PowerShell.Authentication.Properties;
using Microsoft.PowerShell.Commands;

using Newtonsoft.Json;

using DriveNotFoundException = System.Management.Automation.DriveNotFoundException;

namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
Expand Down Expand Up @@ -552,7 +554,10 @@ private HttpResponseMessage GetResponse(HttpClient client, HttpRequestMessage re
}

/// <summary>
/// Set the request content
/// Set the request content.
/// Convert Dictionaries to Json
/// Passing a dictionary to the body object should be translated to Json.
/// Almost everything on Microsoft Graph works converts dictionaries and arrays to JSON.
/// </summary>
/// <param name="request"></param>
/// <param name="content"></param>
Expand All @@ -568,8 +573,8 @@ private long SetRequestContent(HttpRequestMessage request, IDictionary content)
{
throw new ArgumentNullException(nameof(content));
}

var body = content.FormatDictionary();
// Covert all dictionaries to Json
var body = JsonConvert.SerializeObject(content);
return SetRequestContent(request, body);
}

Expand Down Expand Up @@ -668,6 +673,8 @@ private void FillRequestStream(HttpRequestMessage request)
{
content = psBody.BaseObject;
}
// Passing a dictionary to the body object should be translated to Json.
// Almost everything on Microsoft Graph works converts dictionaries and arrays to JSON.
if (content is IDictionary dictionary && request.Method != HttpMethod.Get)
{
SetRequestContent(request, dictionary);
Expand Down Expand Up @@ -703,14 +710,15 @@ private void FillRequestStream(HttpRequestMessage request)
}

// Add the content headers
if (request.Content == null)
// Only Set Content Headers when its not a GET Request
if (request.Content == null && this.Method != GraphRequestMethod.GET)
{
request.Content = new StringContent(string.Empty);
request.Content.Headers.Clear();
}

foreach (var entry in GraphRequestSession.ContentHeaders.Where(header =>
!string.IsNullOrWhiteSpace(header.Value)))
!string.IsNullOrWhiteSpace(header.Value)))
{
if (SkipHeaderValidation)
{
Expand All @@ -724,7 +732,8 @@ private void FillRequestStream(HttpRequestMessage request)
}
catch (FormatException ex)
{
var outerEx = new ValidationMetadataException(Resources.ContentTypeExceptionErrorMessage, ex);
var outerEx =
new ValidationMetadataException(Resources.ContentTypeExceptionErrorMessage, ex);
var er = new ErrorRecord(outerEx, Errors.InvokeGraphContentTypeException,
ErrorCategory.InvalidArgument, ContentType);
ThrowTerminatingError(er);
Expand Down