Lorenzo Durante
Lorenzo Durante

Reputation: 79

PATH_NOT_FOUND when fetching with header

I'm using DummyAPI as the API for my project. This api asks for a header "app-id" for each request. I tried to do that but im getting an error "error: "PATH_NOT_FOUND". Here's my code.

const [post, setPost] = useState([])
const fetchPost = () => {
  fetch(`https://dummyapi.io/data/v1/post`, {
    method:'post',
    headers: {
      'Content-type':'application/json',
      'app-id':'624c9429450430b574dcf17c'
    }
  }) 
    .then(response => {
        return response.json()
    })
    .then(post=>{
        setPost(post)
    })
};
useEffect(()=>{
    fetchPost()
},[])

DummyAPI docs

What i'm i doing wrong here?

Upvotes: 0

Views: 301

Answers (1)

Charfeddine Mohamed Ali
Charfeddine Mohamed Ali

Reputation: 1106

Your problem is that you are using post instead of get :

const [post, setPost] = useState([])
    const fetchPost = () => {
      fetch(`https://dummyapi.io/data/v1/post`, {
        method:'get',
        headers: {
          'Content-type':'application/json',
          'app-id':'624c9429450430b574dcf17c'
        }
      }) 
        .then(response => {
            return response.json()
        })
        .then(post=>{
            setPost(post)
        })
    };
    useEffect(()=>{
        fetchPost()
    },[])

If you check the documentation the request needs a GET HTTP Method enter image description here

Upvotes: 1

Related Questions