0

I have a shortened url: https://dl.flipkart.com/s/XermJqNNNN

which expands to: https://www.flipkart.com/beatxp-prime-deep-tissue-electric-massage-gun-machine-full-body-pain-relief-percussion-massager/p/itmb8e3cc771527c?pid=MASGHMJHYUJCN3WQ&cmpid=product.share.pp&_refId=PP.f843e983-04ea-4b65-b58d-413f2d9ee16f.MASGHMJHYUJCN3WQ&_appId=CL

I want to retrieve this redirected URL.

import requests
def get_original_url(short_url):
    try:
        response = requests.get(short_url, allow_redirects=False)
        if 'Location' in response.headers:
            return response.headers['Location']
        else:
            return "Original URL not found in the response."
    except requests.exceptions.RequestException as e:
        return f"An error occurred: {e}"

I tried the above method which gives me the correct redirect URL in my local system(Windows). But returns the same shortened URL when deployed to the app service on Render(Linux).

4
  • This sounds like a problem with the way you're creating the redirect in the server-side code, not a problem with this code. Commented Aug 12, 2024 at 18:49
  • Hi Barmar, Could you please elaborate on your comment. Is this not the right way to create a redirect within the server? Is there any alternative way to achieve this in the server? Commented Aug 13, 2024 at 5:23
  • I thought the problem you're having is with the response you're getting from requests.get(short_url, allow_redirects=False). So you need to look at the code that responds to the dl.flipkar.com URL, not the code you posted here. Commented Aug 13, 2024 at 16:14
  • This code is just passing along whatever it receives from dl.flipkart.com. Commented Aug 13, 2024 at 16:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.