I am building a React app and in it there's a part where i should set up a profile picture, but every time i call the api i keep getting an error that i haven't filled one of the fields for the post api to return a success response.
this is my jsx
<form>
<input type="file" name="file" onChange={(e) => this.handleFile(e)}></input>
<button type="button" onClick={(e) => this.handleUpload(e)} >send</button>
</form>
and here is the code i'm using to handle those
state = {
file: null
};
handleFile(e) {
console.log(e.target.files, "ssss");
console.log(e.target.files[0], "ssss");
let file = e.target.files
this.setState({ file: e })
}
handleUpload(e) {
let file = this.state.file
let fromdata = new FormData();
fromdata.append('image', file);
fromdata.append('name', "filedata");
const headers = {
Authorization': localStorage.getItem('api_key'),
}
const user = {
filedata: fromdata,
type: 1,
};
axios.post(`sth sth`, user, {
headers: headers
})
.then(res => {
console.log(res);
console.log(res.data);
})
}
so basically the server requires type and filedata but every time i send an api request it keeps returning me
filedata: ["The filedata field is required."]
and i can't seem to find where the problem is.