Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
Invoke-RestMethod Response Headers?
Post Flair (click to view more posts with a particular flair)
Post Body

I'm using PowerShell 5, but I'm assuming the same issue applies to older versions.

I'm making a script to pull data from one of our applications (ConnectWise), which has a REST API. For large datasets, the response is paginated. The first x number of records are returned, and the response header includes a "Link" property that links to the next page of information.

This is an example of the response header (pulled from Postman):

Cache-Control →no-cache
Content-Length →105764
Content-Type →application/json; charset=utf-8
Date →Tue, 12 Jul 2016 19:50:38 GMT
Expires →-1
Link →<https://connectwise.contoso.com/v4_6_release/apis/3.0/company/configurations?conditions=name="SaaS/Hosted Information"&pageSize=25&page=2>; rel="next", <https://connectwise.contoso.com/v4_6_release/apis/3.0/company/configurations?conditions=name="SaaS/Hosted Information"&pageSize=25&page=8>; rel="last"
Pragma →no-cache
Server →Microsoft-IIS/8.5
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET
x-server-name →CONNECTWISE

I can't figure out how to get the response header information from Invoke-RestMethod. I've tried:

  • Setting a session variable, and looking to see if it contains the response headers (no luck)
  • Setting an error variable, and looking to see if it contains response headers (no luck)
  • Looked at all the variables defined in all scopes to see if there is a $response variable or something that holds the response headers (no luck)
  • Looking at the payload returned from Invoke-RestMethod to see if it embeds it in the response somewhere (no luck).

Am I missing something, or does Invoke-RestMethod just discard the response headers entirely? Any ideas on how to get the response headers without writing my own function?\

EDIT: Looks like Invoke-RestMethod just throws away the response headers unless there is an error. Using Invoke-WebRequest is the workaround. Thanks for the help everyone!

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,842
Link Karma
1,262
Comment Karma
12,572
Profile updated: 5 days ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
8 years ago