Skip to content

Commit 5ae2518

Browse files
georgendGeorge
andauthored
Parse HashTables as Json and Handled Powershell 5.0 does not allow GET Requests to have bodies. (#328)
Parse HashTables as Json and Handled Powershell 5.0 does not allow GET Requests to have bodies. Co-authored-by: George <[email protected]>
1 parent 712bbb2 commit 5ae2518

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Microsoft.Graph.PowerShell.Authentication.Properties;
1717
using Microsoft.PowerShell.Commands;
1818

19+
using Newtonsoft.Json;
20+
1921
using DriveNotFoundException = System.Management.Automation.DriveNotFoundException;
2022

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

554556
/// <summary>
555-
/// Set the request content
557+
/// Set the request content.
558+
/// Convert Dictionaries to Json
559+
/// Passing a dictionary to the body object should be translated to Json.
560+
/// Almost everything on Microsoft Graph works converts dictionaries and arrays to JSON.
556561
/// </summary>
557562
/// <param name="request"></param>
558563
/// <param name="content"></param>
@@ -568,8 +573,8 @@ private long SetRequestContent(HttpRequestMessage request, IDictionary content)
568573
{
569574
throw new ArgumentNullException(nameof(content));
570575
}
571-
572-
var body = content.FormatDictionary();
576+
// Covert all dictionaries to Json
577+
var body = JsonConvert.SerializeObject(content);
573578
return SetRequestContent(request, body);
574579
}
575580

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

705712
// Add the content headers
706-
if (request.Content == null)
713+
// Only Set Content Headers when its not a GET Request
714+
if (request.Content == null && this.Method != GraphRequestMethod.GET)
707715
{
708716
request.Content = new StringContent(string.Empty);
709717
request.Content.Headers.Clear();
710718
}
711719

712720
foreach (var entry in GraphRequestSession.ContentHeaders.Where(header =>
713-
!string.IsNullOrWhiteSpace(header.Value)))
721+
!string.IsNullOrWhiteSpace(header.Value)))
714722
{
715723
if (SkipHeaderValidation)
716724
{
@@ -724,7 +732,8 @@ private void FillRequestStream(HttpRequestMessage request)
724732
}
725733
catch (FormatException ex)
726734
{
727-
var outerEx = new ValidationMetadataException(Resources.ContentTypeExceptionErrorMessage, ex);
735+
var outerEx =
736+
new ValidationMetadataException(Resources.ContentTypeExceptionErrorMessage, ex);
728737
var er = new ErrorRecord(outerEx, Errors.InvokeGraphContentTypeException,
729738
ErrorCategory.InvalidArgument, ContentType);
730739
ThrowTerminatingError(er);

0 commit comments

Comments
 (0)