简体   繁体   English

重定向后得到 URL

[英]Get URL after redirect

I'm building a React App in which I send an API get via fetch.我正在构建一个 React 应用程序,我在其中发送一个 API 通过获取获取。 To get the right API response, I need to send a specific link, but users might be able to provide these links in another format that redirect to the correct format if opened in a browser.为了获得正确的 API 响应,我需要发送一个特定的链接,但用户可能能够以另一种格式提供这些链接,如果在浏览器中打开,这些链接将重定向到正确的格式。

Example:例子:

This link https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8此链接https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8
redirects to: https://soundcloud.com/gary-j/ladies-on-mars-kiss-me-baby?p=i&c=0重定向至: https://soundcloud.com/gary-j/ladies-on-mars-kiss-me-baby?p=i&c=0
if opened in a browser.如果在浏览器中打开。

Is there a way in Javascript to get the second link form the first one? Javascript 有没有办法从第一个链接中获取第二个链接?

Thanks!谢谢!

//EDIT: Im both trying to use fetch and request. //编辑:我都在尝试使用获取和请求。 Using request the following way:通过以下方式使用请求:

import React, {Component} from 'react';
const request = require('request');

class Getlink extends Component {
getRequest = () => {
    request('https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8', function (error, response, body) {
    console.error('error:', error); 
    console.log('statusCode:', response && response.statusCode); 
    console.log('body:', body);
});
}

render() {
return (
    {this.getRequest()}
)}
}

which gives the following error这给出了以下错误

Access to fetch at 'https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8' from 
origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-
Control-Allow-Origin' header is present on the requested resource. If an 
opaque response serves your needs, set the request's mode to 'no-cors' to 
fetch the resource with CORS disabled.

plus a GET Failed Error.加上 GET 失败错误。

Is this a normal reaction as I'm doing this from local?这是我在当地做这件事的正常反应吗?

The redirect url is in response.headers.location .重定向 url 在response.headers.location中。 For any redirect response code you could recursively fetch the urls using headers.location .对于任何重定向响应代码,您可以使用headers.location递归地获取 url。

For example, a response to curl shows response code is 302 ( Found ), and the redirect url is specified in the location headers.例如,对curl的响应显示响应代码为302 (已Found ),并且在location标头中指定了重定向 url。

curl -s -o /dev/null -D -  https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8 
HTTP/2 302 
content-type: application/binary
vary: Sec-Fetch-Dest, Sec-Fetch-Mode, Sec-Fetch-Site
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: Mon, 01 Jan 1990 00:00:00 GMT
date: Thu, 08 Oct 2020 10:47:50 GMT
location: https://soundcloud.com/gary-j/ladies-on-mars-kiss-me-baby?ref=clipboard&p=i&c=0
content-security-policy: script-src 'nonce-pxqEOSDMS5AkBDmVHzvHUQ' 'unsafe-inline';object-src 'none';base-uri 'self';report-uri /_/DurableDeepLinkUi/cspreport;worker-src 'self'
server: ESF
content-length: 0
x-xss-protection: 0
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
alt-svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

I figured out some stuff and i guess the problem is related to the website.我想通了一些东西,我想问题与网站有关。 I came across using cors-anywhere which does help to make the request but gives back a 403 Forbidden .我遇到了使用 cors-anywhere 的情况,它确实有助于发出请求,但会返回403 Forbidden You can try here: https://robwu.nl/cors-anywhere.html and entering the url i want to resolve.您可以在这里尝试: https://robwu.nl/cors-anywhere.html并输入我要解决的 url。

fetch("https://cors-anywhere.herokuapp.com/https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8")
        .then((result) => {
        console.log(result)
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM