Arush Singh
Arush Singh

Reputation: 139

Get API response from redux store and send response

What i want to execute is do a post request and access the res.json() and taking res.json() i want to dispatch a action to update the store and access response object in the component.A small code example will be great.

export const filesDownload = (postData) => dispatch =>{
    console.log(postData);
    fetch('http://'+ip+':8000/api/v1/integrator/email/need_attachment/',{
        method:'POST',
        headers: {
            'content-type': 'application/json'
        },
        body : JSON.stringify(postData)
    })
        .then(res => console.log(res.json()))
        .then(files => dispatch({
        type: GET_FILES,
        payload:files
    }))
}

I want to dispatch the res.json() to the store .

Upvotes: 0

Views: 660

Answers (1)

Psartek
Psartek

Reputation: 485

Try axios its cleaner:

export const filesDownload = (postData) => dispatch =>{
  console.log(postData);
    axios.post('http://'+ip+':8000/api/v1/integrator/email/need_attachment/')
    .then((response)=>response.data)
    .then((files)=>dispatch({
    type: GET_FILES,
    payload:files
    }))
    .catch((err)=>console.log(err))
}

If it's not working, you will have to use redux Thunk, tell me, I will help you

Upvotes: 1

Related Questions