I need to send some information to an API using JSON. Among this information will be a base64 array, but that array is getting empty.
In my code I have an array of files with images:
[0: File {name: '766_generated.jpg', lastModified: 1613057822000, lastModifiedDate: Thu Feb 11 2021 12:37:02 GMT-0300 (Horário Padrão de Brasília), webkitRelativePath: '', size: 191010, …}]
In my function I make a map in the image array, transform it into base64 and put it in another array:
let filesBody = []
this.files.map(item => {
var reader = new FileReader();
reader.readAsDataURL(item);
reader.onload = function () {
let result = reader.result
return filesBody.push(result)
};
})
console.log(filesBody):
[0: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD…AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf/Z"]
body:
let body = { "files": filesBody }
console.log(body):
{
"files": ['data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD…AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf/Z']
}
POST:
this.billingService.sendBillingRevaluation(body)
service:
sendBillingRevaluation(body): Observable<any> {
return this.http.post<any>(
`${ServicesUrls.URL}/queries/sendBillingRevaluation`,
body
)
}
After doing this to get the base64 array I send the body by POST but the base64 array is being sent empty:
{
"files": []
}
To identify that it was being sent empty, I used two ways: network tab in browser and endpoint log in backend
Is there any different way to send base64 to JSON?