Skip to content

Updates Invoke-MgGraphRequest cmdlet to handle responses of text/plain content types. #2896

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
Show file tree
Hide file tree
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 @@ -453,6 +453,10 @@ internal async Task ProcessResponseAsync(HttpResponseMessage response)
throw new ArgumentOutOfRangeException(nameof(OutputType));
}
break;
case RestReturnType.PlainText:
responseString = await response.Content.ReadAsStringAsync();
WriteObject(responseString);
break;
case RestReturnType.OctetStream:
if (OutputType == OutputType.HttpResponseMessage)
WriteObject(response);
Expand Down
4 changes: 4 additions & 0 deletions src/Authentication/Authentication/Helpers/ContentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal static RestReturnType CheckReturnType(this HttpResponseMessage response
rt = RestReturnType.Image;
else if (IsOctetStream(contentType))
rt = RestReturnType.OctetStream;
else if (IsPlainText(contentType))
rt = RestReturnType.PlainText;
return rt;
}

Expand Down Expand Up @@ -80,6 +82,8 @@ private static bool CheckIsOctetStream(string contentType)
return isOctetStream;
}

private static bool IsPlainText(string contentType) => contentType.Equals("text/plain", StringComparison.OrdinalIgnoreCase);

// used to split contentType arguments
private static readonly char[] ContentTypeParamSeparator = { ';' };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public enum RestReturnType
/// <summary>
/// image/* (image/jpeg, image/png) return type
/// </summary>
Image = 4
Image = 4,
/// <summary>
/// text/plain return type
/// </summary>
PlainText = 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Describe 'Invoke-MgGraphRequest Command' -skip {
It 'Should not throw when -InferOutputFilePath is specified' {
{ Invoke-MgGraphRequest -OutputType PSObject -Uri "https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityUserDetail(period='D7')" -InferOutputFileName } | Should -Not -Throw
}
It 'Should return plain text' {
{ Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/applications/`$count" } | Should -BeOfType [System.String]
}
}

Context 'Absolute and relative URIs' {
Expand Down
Loading