Home > Enterprise >  im getting 'Request failed with status code 404' and bad_request error in my reactjs proje
im getting 'Request failed with status code 404' and bad_request error in my reactjs proje

Time:08-10

const [state, setState] = useState([]);

 React.useEffect(() => { 


    axios
      .get(process.env.REACT_APP_API_BASE_URL)
       .then(response => {setState(response.data)});



    },[]);

CodePudding user response:

Couple of things

  1. It looks like you are just hitting the BASE URL. In a lot of cases, the BASE_URL is just that, the base url. Your actual API call generally contains the end point you are trying to invoke. For example, your API BASE URL can be something like 'http://localost:3002' and your endpoint can be '/users', which means you need to send axios.get(`${process.env.REACT_APP_API_BASE_URL}/users`).
  2. Check what is the response you get from the request. From the looks of it, you are calling an API that doesn't exist(refer 1), or you are not passing the query params that your API is expecting.

CodePudding user response:

I can't tell nothing with only that code, it seems right. I would try checking if the variable REACT_APP_API_BASE_URL is giving the correct format, if I am not wrong for axios to work correctly it needs to include "http://" in the ip parameter and it might need to include a port in the IP, for example:

axios.get("http://192.168.0.1:3000/example").then(response => {setState(response.data)}); },[]);

CodePudding user response:

Because your current URL does not have something to get or displayed so first try to find out the actual URL. For E.g Your Base URL is http://localhost:3000 which you kept inside the process.env.REACT_APP_API_BASE_URL so Let's suppose you're getting the list of users which has the URL of http://localhost:3000/users. So inorder to fetch that data you have to code like this:

React.useEffect(() => {

axios.get(process.env.REACT_APP_API_BASE_URL    "/user")
 .then(response => {setState(response.data)});


},[]);
  • Related