Home > front end >  Getting the info from exact header using wget
Getting the info from exact header using wget

Time:07-20

I need to get data from Apixu using PowerShell and wget command. So, I tried this variant

wget -UseBasicParsing "http://api.weatherstack.com/current?access_key=MY_KEY&query=London"

and it kinda works, but not the exact way I want it to work. The output is:

StatusCode        : 200
StatusDescription : OK
Content           : {"request":{"type":"City","query":"London, United
                    Kingdom","language":"en","unit":"m"},"location":{"name":"London","country":"United
                    Kingdom","region":"City of London, Greater London","lat":"51.517","...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    access-control-allow-methods: GET, HEAD, POST, PUT, PATCH, DELETE,...
Forms             :
Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [x-apilayer-transaction-id], [access-control-allow-methods, GET, HEAD, POST, PUT, PATCH,
                    DELETE, OPTIONS]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 722

So, is there a way to print value of "Content" header to console?

CodePudding user response:

You can take the value of any single property by using ForEach-Object -MemberName:

PS ~> Invoke-WebRequest -UseBasicParsing "http://api.weatherstack.com/current?access_key=MY_KEY&query=London" |ForEach-Object -MemberName Content
{
  "success": false,
  "error": {
    "code": 101,
    "type": "invalid_access_key",
    "info": "You have not supplied a valid API Access Key. [Technical Support: [email protected]]"
  }
}

You can also pipe the response object directly to ConvertTo-Json which will construct a custom object as described by the JSON - ConvertTo-Json will automatically figure out to parse only the Content value:

PS ~> Invoke-WebRequest -UseBasicParsing "http://api.weatherstack.com/current?access_key=MY_KEY&query=London" |ConvertTo-Json

success error
------- -----
  False @{code=101; type=invalid_access_key; info=You have not supplied a valid API Access Key....
  • Related