App.js
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Background
I am currently rendering a C3.js
chart in a webpage. I need to have the image representation of that chart so I can display it in other ways throughout my app. In order to do that I am converting the SVG
to canvas
with html2canvas
and then converting that to a blob
which I then post to an express server.
When I check the network tab I can see the blob
is in the payload
section. But, on the server when I console.log(req.body)
it outputs an empty object {}
.
Example
Client Side
function setImage() {
html2canvas(document.querySelector('#chart')).then(canvas => {
canvas.toBlob(
function(blob) {
const url = 'http://localhost:3000/api/v1/pdf';
fetch(url, {
method: 'POST',
headers: {},
body: blob,
})
.then(response => response.json())
.then(success => console.log(success))
.catch(error => console.log(error));
console.log(blob);
},
'image/jpeg',
1,
);
document.body.appendChild(canvas);
});
}
Server Side
router.post('/', (req, res, next) => {
console.log(req.body);
res.json({ message: 'Something good happened' });
});
Things I have tried
JSON.stringify(blob)
Different headers but I also read that leaving it blank should also work,
headers: new Headers({ 'Content-Type': 'application/json' })
In the network tab from chrome dev tools
The terminal output from the express route
When I console.log(blob)
it looks like an object,
What is the proper way to POST the blob and verify that I have access to it on the server?