Requests with arguments

Arguments are passed to REST methods in one of two possible ways: in a query string or using the Body parameter.

The Google geocoding API expects address as an argument:

https://developers.google.com/maps/documentation/geocoding/start

When using a query string any reserved characters must be replaced. For example, spaces in a query string must be replaced with %20 or +. The .NET framework provides a means of changing reserved characters using the HttpUtility class:

# HttpUtility is not available without loading System.Web 
Add-Type -AssemblyName System.Web 
$address = '221b Baker St, Marylebone, London NW1 6XE' 
$address = [System.Web.HttpUtility]::UrlEncode($address) 
Invoke-RestMethod -Uri "https://maps.googleapis.com/maps/api/geocode/json?address=$address" 

These additional steps may be avoided by using the Body parameter of the Invoke-RestMethod. Any encoding changes that may be required will be handled automatically:

$body = @{ 
address = '221b Baker St, Marylebone, London NW1 6XE' 
} 
Invoke-RestMethod -Uri "https://maps.googleapis.com/maps/api/geocode/json" -Body $body 

In this case, the arguments are described by a hashtable.

The previous syntax is much easier to work with than a long query string, but it is not necessarily clear this is possible from the developer guides for REST interfaces.

It is critical to note that REST interfaces are case sensitive; using a parameter named Address would result in an error message as shown following:

PS> $body = @{ 
    Address = '221b Baker St, Marylebone, London NW1 6XE' 
} 
Invoke-RestMethod -Uri "https://maps.googleapis.com/maps/api/geocode/json" -Body $body 
Invoke-RestMethod : The remote server returned an error: (400) Bad Request. 
At line:4 char:1 
+ Invoke-RestMethod -Uri "https://maps.googleapis.com/maps/api/geocode/ ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.72.176