简体   繁体   中英

Getting the info from exact header using wget

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?

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: support@apilayer.com]"
  }
}

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM