PowerShell – Automatically follow redirects

Welcome Forums General PowerShell Q&A PowerShell – Automatically follow redirects

Viewing 1 reply thread
  • Author
    • #222168
      Topics: 2
      Replies: 0
      Points: 12
      Rank: Member

      HiĀ  everyone,

      To explain you the design, I’ve built an API for Amazon AMS. I export data automatically.

      I now have a problem with Automatically follow redirects. When I run my script via POSTMAN, I can download my data, because I could disable the Automatically follow redirects option in the POSTMAN software, so my GET request uses the HTTP 307 redirect.

      So I tried in powershell to do the same method:
      Here is my command line:

      $response = Invoke-RestMethod ‘https://advertising-api-eu.amazon.com/v2/reports/amzn1.clickXXXX/download’ -Method ‘GET’ -Headers $headers -MaximumRedirection 0

      But I have nothing, no data, I thought MaximumRedirection 0 allowed to do as in POSTMAN but I don’t understand anymore.

      Do you have ideas to help me?

      I give you in screenshot the PSOTMAN parameter that allowed me to get what I want.


      Thank you in advance.

    • #222177
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      I’m assuming it’s a typo that there is a semi-colon at the end of the uri, otherwise it would be considered the end of the line and you’d be seeing other errors. Here are some links I found:

      PowerShell – Capturing a Redirected URL from a Web Request


      The documentation does state that 0 should stop all redirection:

      Specifies how many times PowerShell redirects a connection to an alternate Uniform Resource Identifier (URI) before the connection fails. The default value is 5. A value of 0 (zero) prevents all redirection.

      If you want to see what headers are coming back, you typically need to use Invoke-WebRequest as Invoke-RestMethod doesn’t return header information. The latest version of Powershell is supposed have some capabilities to see response codes, but haven’t played with it.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.