Reputation: 79
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()
},[])
What i'm i doing wrong here?
Upvotes: 0
Views: 301
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
Upvotes: 1