I try to send base64 string via axios post request
const sendData = async () => {
if (newImg) {
console.log(fileSizeInBytes(newImg));
const result = await axios.post(
`${process.env.REACT_APP_API}/uploadData`,
{
type: "image",
data: newImg,
userId: user._id,
},
{
headers: {
authtoken,
},
}
);
try {
console.log("data", result.data);
setImageUrl(result.data.url);
} catch (error) {
console.log("error", error);
}
}
}
Here I have a function which calculates base64 string size:
function fileSizeInBytes(base64) {
const base64String = base64.replaceAll("=", "");
const bytes = base64String.length * (3 / 4);
return bytes;
}
But the base64 string is sent only if it’s size is not bigger than ca 76456 bytes. The problem is clearly with the size of the string-if it’s smaller then it gets through.
Here is also my serverside setup
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
dotenv.config();
const bodyParser = require("body-parser");
const cors = require("cors");
const authRoutes = require("./routes/auth");
const userRoutes = require("./routes/user");
const cloudinaryRoutes = require("./routes/cloudinary");
const chatRoutes = require("./routes/chat");
const messageRoutes = require("./routes/message");
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log("DB connected!!"));
mongoose.connection.on("error", (err) => {
console.log(`DB connection error: ${err.message}`);
});
app.use(bodyParser.json());
app.use(cors());
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", cloudinaryRoutes);
app.use("/api", chatRoutes);
app.use("/api", messageRoutes);
app.use(function (err, req, res, next) {
if (err.name === "UnauthorizedError") {
res.status(401).json({
error: "Unauthorized",
});
}
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
"Server is up and running on port " + port;
});
Is it an axios problem? What am I doing wrong here?