I'm sending a post request from vue.js project using Axios and it contains a file upload, which requires me to use FormData, I found a nice answer that helped me with FormData:
const getFormData = object => Object.keys(object).reduce((formData, key) => {
formData.append(key, object[key]);
return formData;
}, new FormData());
and for the headers: headers: { 'Content-Type': 'multipart/form-data'}.
The POST call looks like this:
axios.post("http://127.0.0.1:8000/api/document/",
getFormData(this.documentData),
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(response => {
console.log("Successfully uploaded: ", response.data)
})
.catch(err => {
console.log("error occured: ", err)
})
This is the data I'm sending:
documentData: {
name: '',
file: '',
version: '',
company: '',
author: '',
category: []
}
When sending the data with single category id, it works fine, but when I send multiple category ids the following error shows:
"category": [
"Incorrect type. Expected pk value, received str."
]
How can I solve this problem?
category? Is it expecting a JSON array or multiplecategoryentries?