Skip to content

Body to Query Parameters in GET Requests with InvokeMgGraphRequest #437

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

Closed
ryanspletzer opened this issue Oct 20, 2020 · 2 comments
Closed
Assignees
Milestone

Comments

@ryanspletzer
Copy link

It appears here that the body for GET requests with InvokeMgGraphRequest is translated into query string params. Is this for ease of use in PowerShell? Would it be possible to document this in the help message for Body? I hadn't noticed this at all until I was digging into the code. :)

private Uri PrepareUri(HttpClient httpClient, Uri uri)
{
// before creating the web request,
// preprocess Body if content is a dictionary and method is GET (set as query)
if (Method == GraphRequestMethod.GET && LanguagePrimitives.TryConvertTo(Body, out IDictionary bodyAsDictionary))
{
UriBuilder uriBuilder;
// For AbsoluteUri such as /beta/groups$count=true, Get the scheme and host from httpClient
// Then use them to compose a new Url with the URL fragment.
if (!uri.IsAbsoluteUri)
{
uriBuilder = new UriBuilder
{
Scheme = httpClient.BaseAddress.Scheme,
Host = httpClient.BaseAddress.Host
};
var newAbsoluteUri = new Uri(uriBuilder.Uri, uri);
uriBuilder = new UriBuilder(newAbsoluteUri);
}
else
{
uriBuilder = new UriBuilder(uri);
}
var bodyQueryParameters = bodyAsDictionary?.FormatDictionary();
if (uriBuilder.Query != null && uriBuilder.Query.Length > 1 && !string.IsNullOrWhiteSpace(bodyQueryParameters))
{
uriBuilder.Query = uriBuilder.Query.Substring(1) + "&" + bodyQueryParameters;
}
else if (!string.IsNullOrWhiteSpace(bodyQueryParameters))
{
uriBuilder.Query = bodyQueryParameters;
}
uri = uriBuilder.Uri;
// set body to null to prevent later FillRequestStream
Body = null;
}
return uri;
}

@ghost ghost added the ToTriage label Oct 20, 2020
@ddyett ddyett added this to the Post GA milestone Oct 21, 2020
@ryanspletzer
Copy link
Author

@georgend
Copy link
Contributor

georgend commented Oct 22, 2020

@ryanspletzer Yes, the cmdlet was inspired by Invoke-RestMethod and Invoke-WebRequest as requested by our customers.

Related to #127 #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants