Skip to content

Commit 437e8bf

Browse files
georgendGeorge Ndungu
and
George Ndungu
authored
Set default Content-Type header to application/json when user has not specified -ContentType for PUT,POST,PATCH. (#486)
Co-authored-by: George Ndungu <[email protected]>
1 parent 390706e commit 437e8bf

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ public InvokeMgGraphRequest()
228228

229229
internal bool ShouldCheckHttpStatus => !SkipHttpErrorCheck;
230230

231+
/// <summary>
232+
/// Only Set Default Content Type (application/json) for POST, PUT and PATCH requests, where its not specified via `-ContentType`.
233+
/// </summary>
234+
private bool ShouldSetDefaultContentType => Method == GraphRequestMethod.POST || Method == GraphRequestMethod.PUT || Method == GraphRequestMethod.PATCH;
235+
231236
private static async Task<ErrorRecord> GenerateHttpErrorRecordAsync(
232237
HttpMessageFormatter httpResponseMessageFormatter,
233238
HttpRequestMessage httpRequestMessage)
@@ -606,10 +611,10 @@ private long SetRequestContent(HttpRequestMessage request, string content)
606611

607612
Encoding encoding = null;
608613
// When contentType is set, coerce to correct encoding.
609-
if (ContentType != null)
614+
if (!string.IsNullOrWhiteSpace(ContentType))
610615
{
611616
// If Content-Type contains the encoding format (as CharSet), use this encoding format
612-
// to encode the Body of the WebRequest sent to the server. Default Encoding format
617+
// to encode the Body of the GraphRequest sent to the server. Default Encoding format
613618
// would be used if Charset is not supplied in the Content-Type property.
614619
try
615620
{
@@ -661,7 +666,7 @@ private void FillRequestStream(HttpRequestMessage request)
661666
{
662667
GraphRequestSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType;
663668
}
664-
else if (Method == GraphRequestMethod.POST)
669+
else if (ShouldSetDefaultContentType)
665670
{
666671
GraphRequestSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentType);
667672
if (string.IsNullOrWhiteSpace(contentType))

0 commit comments

Comments
 (0)