question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to retrieve the penultimate redirected URL

See original GitHub issue

Hi,

This has been discussed before somewhat in a previous thread (https://github.com/node-fetch/node-fetch/issues/978). I have what I thought was a very simple problem that I am struggling to resolve. Full disclosure, I am very new to coding.

I am trying to GET the redirected temporary URL with status 302 code that displays just prior to my webpage. I then need to parse the URL to get a certain query parameter for a subsequent POST call.

My app is built with ReactJS as frontend and express/nodejs as backend.

I have noted the below https://github.com/node-fetch/node-fetch/issues/978

but what I am struggling with is to understand how to get this code to run when I am on subdirectory “/webpage”.

This may be very very wrong, but what I have attempted is the below. I’ve adapted the code from the above thread as a function

export var url  = `https://<mywebpage>/webpage`;

function script () {
    var response = fetch(previousURL, {
        method: 'GET',
    }).then(response => {
        console.log(response.url).else(error => console.log(error))});

    return (response.url);
};

export default script();

I have then written the below code so this function loads when my “/webpage/” loads:

const express = require('express');
const app = express();
const script = require("./script.js");

app.get('/webpage', (req, res) => {
    script();
})

app.listen(3000);

Really appreciate any help 😃))

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
sofian-ysfcommented, Oct 7, 2022

Hi Jimmy,

Thanks for the suggestion. I’ve tried the above, but I’m still not getting the previous (status 302) redirect url returned.

2reactions
jimmywartingcommented, Oct 5, 2022

I guess you are looking for:

const res = await fetch(url, {
  redirect: 'manual'
})
res.headers.get('location')
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Getting the Redirected URL from the Original URL
Use this code to get redirecting URL public void GrtUrl(string url) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.
Read more >
Get Final URL After a Redirect | Baeldung on Linux
URL stands for Uniform Resource Locator – it's the unique address of a resource on the web. · We use a curl command...
Read more >
Redirections in HTTP - MDN Web Docs - Mozilla
Permanent redirects to preserve existing links/bookmarks after changing the site's URLs, progress pages when uploading a file, etc. Principle.
Read more >
WhereGoes: Tiny URL Expander | Redirect Checker
It will show you the full redirection path of URLs, shortened links, or tiny URLs. Also referred to as a link checker, url...
Read more >
URL Redirects - Squarespace Help Center
Invalid mapping: Expected to find 301 or 302 ... This means that there's an error in your redirect type. Only 301 and 302...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found