On my API I created the endpoint http://XX.XXX.XX.XX/api/v1/endpoint which receives this string :
{
"user": "RANDOM USER",
"url": "RANDOM URL",
"keyWord": "RANDOM KEYWORD"
}
and returns a result. It works GREAT on postman but on the frontend part of the project I'm getting an error, and I can't seem to figure out why. At first I thought that it has something to do with authentication but I changed the API so there is no need to be signed in anymore, but it still doesn't work on the react project. (works in postman though). This is the error:
This is the frontend code:
import axios from 'axios';
const user = "RANDOM USER";
const url = "RANDOM URL";
const keyWord = "RANDOM KEYWORD";
const test = async () => {
const details = JSON.stringify({
user,
url,
keyWord,
});
try {
await axios
.get('http://XX.XXX.XX.XX/api/v1/endpoint', details, {
headers: {
'Content-Type': 'application/json',
},
})
.then((res) => {
console.log(JSON.stringify(res.data));
})
.catch((err) => {
if (err.response) {
// console.log(details); //WORKS
console.log(err.response.data.error);
}
});
} catch (e) {
console.log(e);
}
This is the backend code:
exports.test = catchError(async (req, res, next) => { //catchError is an outer function with try-catch, it catches errors and it works fine
const { url, keyWord } = await req.body;
const response = await axios.get(url);
const html = await response.data;
res.status(200).json({
status: 'success',
data: {
'XXXX'
}
});
Any ideas? Please help!