PowerShell – Automatically follow redirects

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

Viewing 1 reply thread
  • Author
    Posts
    • #222168
      Participant
      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.

      https://ibb.co/wr5F2Sc

      Thank you in advance.
      Maxime

    • #222177
      Participant
      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


      https://4sysops.com/archives/how-to-test-for-a-http-url-redirect-in-powershell/

      The documentation does state that 0 should stop all redirection:

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