Search code examples
jsonpowershellapiweather-api

How do I fix a "cannot bind parameter 'uri'. Cannot convert the...." when converting json in powershell?


I am doing a Get-Weather project in powershell, where I pull data down from weatherapi.com . I am able to successfully connect to the website using an API key but, when I try to convert it from json in the script it doesn't work. The error I get is:

"Cannot bind parameter 'Uri'. Cannot convert the..." I have tried so many different ways to write this:

$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s

$weatherobject = ConvertFrom-Json $url

The request for the website is:

$url = Invoke-WebRequest "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"

Any help would be very much apperciated, thank you!


Solution

  • The input of the ConvertFrom-Json cmdlet is a JSON object. Look at the document below for more information

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1#description

    $url = "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
    $response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
    $weatherobject = ConvertFrom-Json $response