I’m currently creating a 3rd party webapp which fetches from an api. when using postman and providing the correct headers, I get all the information I need, but when I try to make postman issue the request on my backend server, which then asks for the info, I get an unauthorized error.
Normally on Postman I do a get request with ‘the link’ and then pass in the headers, authorization and x-auth-token, and this outputs the right data. When switching over to my backend server that runs on localhost:3001,I instead ask postman to make a get request from ‘localhost:3001/search’ and pass the same exact headers as the working postman code
Here is my code:
server.js
const express = require('express');
const bookings = require('./routes/bookings.js');
const requests = require('./routes/requests');
const search = require('./routes/search');
const auth = require('./routes/auth')
const cookieParser = require('cookie-parser')
const mongoose = require('mongoose')
const server = express();
const port = 3001
// set bodyparser
server.use(express.json());
server.use(cookieParser())
server.use(express.urlencoded({extended: true}))
server.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
mongoose.connect(
process.env.DBHOST,
{
useUnifiedTopology: true,
useNewUrlParser: true
}
).catch(error => console.log(error));
server.use('/bookings', bookings);
server.use('/requests', requests);
server.use('/search', search);
server.use('/user', auth)
mongoose.connection.once('open', () => console.log("Connected succesfully to MongoDB"))
module.exports = server
search.js
const express = require('express')
const router = express.Router()
const axios = require('axios')
require("dotenv").config({ silent: true })
router.get("/",(req,res) => {
axios
.get(`the link`)
.then(apiResponse => {
res.json(apiResponse.data)
})
})
module.exports = router;
this returns a 419 response Unauthorized error. I’ve also tried making my front-end pass the same headers to my back-end server through another axios call, but this is to no avail. Any help would be greatly appreciated.