'making post request with createAsyncThunk redux toolkit
Following is my reduxtoolkit code. I am trying to post data but it is giving me 422 error. The response says spot_id and item_id required but I am sending it. Am i doing anthing wrong?
export const postData = createAsyncThunk(
'items/addItem',
async (data, thunk) => {
const accessToken = await getStorageValue("accessToken")
const axios = await axiosMaker()
const newItem = new FormData()
newItem.append("spot_id", data.spotId)
newItem.append("item_id", JSON.stringify(data.itemId))
try {
const response = await axios.post('add_item',newItem , {
headers: {
authorization: `Bearer ${accessToken}`,
},
})
console.log("it is success",response.data)
return response.data
} catch (error) {
console.log("it is failure",error)
return thunk.rejectWithValue("error")
}
}
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|