Skip to content

Bug: Invoke-GraphRequest fails with BadRequest for PUT Requests #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public InvokeMgGraphRequest()

internal bool ShouldCheckHttpStatus => !SkipHttpErrorCheck;

/// <summary>
/// Only Set Default Content Type (application/json) for POST, PUT and PATCH requests, where its not specified via `-ContentType`.
/// </summary>
private bool ShouldSetDefaultContentType => Method == GraphRequestMethod.POST || Method == GraphRequestMethod.PUT || Method == GraphRequestMethod.PATCH;

private static async Task<ErrorRecord> GenerateHttpErrorRecordAsync(
HttpMessageFormatter httpResponseMessageFormatter,
HttpRequestMessage httpRequestMessage)
Expand Down Expand Up @@ -606,10 +611,10 @@ private long SetRequestContent(HttpRequestMessage request, string content)

Encoding encoding = null;
// When contentType is set, coerce to correct encoding.
if (ContentType != null)
if (!string.IsNullOrWhiteSpace(ContentType))
{
// If Content-Type contains the encoding format (as CharSet), use this encoding format
// to encode the Body of the WebRequest sent to the server. Default Encoding format
// to encode the Body of the GraphRequest sent to the server. Default Encoding format
// would be used if Charset is not supplied in the Content-Type property.
try
{
Expand Down Expand Up @@ -661,7 +666,7 @@ private void FillRequestStream(HttpRequestMessage request)
{
GraphRequestSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType;
}
else if (Method == GraphRequestMethod.POST)
else if (ShouldSetDefaultContentType)
{
GraphRequestSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentType);
if (string.IsNullOrWhiteSpace(contentType))
Expand Down