{"id":40398,"date":"2021-08-31T10:08:54","date_gmt":"2021-08-31T17:08:54","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/premier-developer\/?p=40398"},"modified":"2021-08-31T10:08:54","modified_gmt":"2021-08-31T17:08:54","slug":"deconstructing-azure-powershell-apis-with-fiddler","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/deconstructing-azure-powershell-apis-with-fiddler\/","title":{"rendered":"Deconstructing Azure PowerShell APIs with Fiddler"},"content":{"rendered":"<p><a href=\"https:\/\/www.linkedin.com\/in\/reedrobison\/\" target=\"_blank\" rel=\"noopener\">Reed Robison<\/a> shows how to deconstruct Azure PowerShell operations to understand how the underlying REST calls work.<\/p>\n<hr \/>\n<p>In my previous <a href=\"https:\/\/devblogs.microsoft.com\/premier-developer\/getting-started-with-graph-api-and-graph-explorer\/\">post<\/a>, I talked about how <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/graph-explorer\">Graph Explorer<\/a> and <a href=\"https:\/\/www.telerik.com\/fiddler\">Fiddler<\/a> can be helpful tools when working with REST APIs. Both tools do a good job of showing you the details of request and response payloads.<\/p>\n<h3>Let\u2019s look at a practical example of how this comes in handy.<\/h3>\n<p>There are a ton of great PowerShell libraries out there that are super easy to use. Most of these just sit on top of standard REST APIs and abstract all the details away. Recently, I ran into a use case where we couldn\u2019t use <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/azure\/?view=azps-6.3.0\">Azure PowerShell<\/a> and were looking to make equivalent calls using the <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/azure\/\">REST APIs<\/a> directly. In a perfect world, there would be plenty of samples to pull from \u2013 but that\u2019s not always the case. You may also find that there is not always an obvious 1:1 mapping of the parameters when you move between PowerShell and REST APIs.<\/p>\n<p>In this case, we were using Set-AzAnalysisServicesServer and were making use of the DefaultConnectionMode param. So how do you map this into the REST equivalents?<\/p>\n<p>With a little digging into <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/analysis-services\/analysis-services-server-admins\">management operation options<\/a> for Analysis Services, we find the logical equivalent would be the REST <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/analysisservices\/servers\/update\">Update<\/a> operation.<\/p>\n<p>In the PowerShell version, DefaultConnectionMode is simply described as the \u201cDefault connection mode of an Analysis service server\u201d. In the REST docs for Update, we find something called properties.querypoolConnectionMode which looks similar, but it\u2019s defined as \u201cHow the read-write server&#8217;s participation in the query pool is controlled.\u201d It takes a <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/analysisservices\/servers\/update#connectionmode\">ConnectionMode<\/a> values of All or ReadOnly, but I couldn&#8217;t find any examples. In my experience, it\u2019s common for there to be small disparities between the PowerShell APIs and the REST APIs like this. Furthermore, it\u2019s sometimes frustrating trying to figure out the \u201cright\u201d payload syntax if there isn\u2019t a sample in the docs that use all the params you need to build on.<\/p>\n<p>What else could we do to verify parameter equivalents? Where does querypoolConnectionMode go in the request body? <em>Fiddler to the rescue.<\/em><\/p>\n<p>If I want to see how a PowerShell command works underneath the hood, I could either go find the source on GitHub and <a href=\"https:\/\/github.com\/Azure\/azure-powershell\">dig into the code<\/a> \u2013 but I\u2019m really just curious about how it\u2019s making the REST calls. For me, it\u2019s more efficient to examine the calls than trace through all the code. To do this, I fire up Fiddler and examine the trace for Set-AzAnalysisServicesServer.<\/p>\n<h3>Breaking down the Azure PowerShell API to see what it&#8217;s doing<\/h3>\n<p>If I execute the following PowerShell call to perform a basic operation using DefaultConnectionMode it looks something like this:<\/p>\n<p><strong>Set-AzAnalysisServicesServer -Name \u201creedras\u201d -ResourceGroupName \u201creredrasg\u201d -Administrator \u201creedr@microsoft.com\u201d -DefaultConnectionMode All<\/strong><\/p>\n<p>Looking at the Fiddler trace when I run this command, I can see it break down into three separate REST calls.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/fiddler2.png\"><img decoding=\"async\" class=\"alignnone wp-image-40407 size-full\" src=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/fiddler2.png\" alt=\"Image fiddler2\" width=\"800\" height=\"145\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/fiddler2.png 800w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/fiddler2-300x54.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/fiddler2-768x139.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>The PowerShell API issues a couple of GET calls to retrieve information on the server, and then issues a PATCH call to make any changes. It\u2019s simply abstracting REST calls behind the scenes, but I can examine the request payload to see exactly what its sending \u2013 which is basically the information I need to build out my own REST call correctly.<\/p>\n<p><img decoding=\"async\" width=\"535\" height=\"479\" class=\"wp-image-40400\" src=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-1.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-1.png 535w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-1-300x269.png 300w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/p>\n<p>The parameter I\u2019m interested in is DefaultConnectMode which I set to All. If I change it to use the ReadOnly param and re-issue the call, I can see it reflected in the payload again \u2013 and as I suspected, it maps to the querypoolConectionMode param in the REST version of the call and I can see exactly how the payload is constructed.<\/p>\n<p><img decoding=\"async\" width=\"511\" height=\"422\" class=\"wp-image-40401\" src=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-2.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-2.png 511w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2021\/08\/word-image-2-300x248.png 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/p>\n<p>When you are building code to use REST APIs, a working example of the call can save a lot of time. If you cannot find an example or the documentation doesn\u2019t answer all your questions, it\u2019s easy to use Fiddler and a PowerShell API to generate your own sample of a working request payload.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a ton of great PowerShell libraries out there that are super easy to use. Most of these just sit on top of standard REST APIs and abstract all the details away.   When you are building code to use REST APIs, it\u2019s easy to use Fiddler and a PowerShell API to generate your own sample of a working request payload.<\/p>\n","protected":false},"author":582,"featured_media":40402,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25,5721],"tags":[227,324],"class_list":["post-40398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-powershell","tag-graphapi","tag-rest"],"acf":[],"blog_post_summary":"<p>There are a ton of great PowerShell libraries out there that are super easy to use. Most of these just sit on top of standard REST APIs and abstract all the details away.   When you are building code to use REST APIs, it\u2019s easy to use Fiddler and a PowerShell API to generate your own sample of a working request payload.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/40398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=40398"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/40398\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/40402"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=40398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=40398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=40398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}