From 4bb7e0fb353b2d34c1a9e7642b5c51c60a9c00f1 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 12 Aug 2020 19:33:00 +0300 Subject: [PATCH] Powershell 5.0 does not allow GET Requests to have bodies. Parse HashTables as Json. --- .../Cmdlets/InvokeGraphRequest.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs index 0844984556e..6268868a59e 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs @@ -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 @@ -552,7 +554,10 @@ private HttpResponseMessage GetResponse(HttpClient client, HttpRequestMessage re } /// - /// 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. /// /// /// @@ -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); } @@ -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); @@ -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) { @@ -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);