Calling a url in chrome browser or in postman working fine but using axios it is giving 404 error

25 Views Asked by At

i have a url and when i called that url using browsers and postman it is working fine but using axios it is throwing 404 error,i will be grateful if someone help me out to clear this error

iam giving the snippet of code where iam facing problem

let response = await axios.get(`https://scholar.google.co.in/citations?user=sGJSZ1AAAAAJ&hl=en&cstart=0&pagesize=1000`, {
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
    },
  });

i checked whether url is correct or not then i found that there is no mistake in url then i added the user-agent header but no use

1

There are 1 best solutions below

2
Gleb Gorokhov On

Your request is being blocked by the CORS policy.

For the local development you can use this extension:

But the right solution is to set up a basic proxy server for your requests. This is how you can do it:

  1. Create a new folder, open the terminal inside
  2. Use npm init to create a package.json file
  3. Set "type": "module" in package.json
  4. Run npm i cors express node-fetch
  5. Create an index.js file and put this inside:
import express from "express";
import cors from "cors"
import fetch from "node-fetch";

const app = express();
app.use(cors());

const requestListener = async function (req, res) {
  const response = await fetch("https://scholar.google.co.in/citations?user=sGJSZ1AAAAAJ&hl=en&cstart=0&pagesize=1000")
  const data = await response.text();
  res.send(data);
}

app.get('/', cors(), requestListener);

app.listen(2000, () => {
  console.log('Start listen on port 2000');
})
  1. Run node index.js in your terminal
  2. Then do a request to this server like that:
let response = await axios.get("http://localhost:2000")

This will return you the HTML of that page.