By default axios uses Json for posting data so you don't need to stringify your data. The problem could be that you're doing that. Could you try doing the post without it and check if it works? Also you don't need the curly braces to wrap your data unless that's the format of the object in your server. Otherwise could you give me information about how the body of the request looks like so I have more context? You can check that in chrome dev tools using the network tab
Answer from Santiago Benitez on Stack Overflowhow can i axios post json data same as jq post json data?
Axios posts as JSON object, how to change
Post a number as a JSON
JSON Post with arrays
Videos
By default axios uses Json for posting data so you don't need to stringify your data. The problem could be that you're doing that. Could you try doing the post without it and check if it works? Also you don't need the curly braces to wrap your data unless that's the format of the object in your server. Otherwise could you give me information about how the body of the request looks like so I have more context? You can check that in chrome dev tools using the network tab
You don't need to stringify your payload. Axios will do it for you when it it send a request.
const dt = { data: { value: "gdfg1df2g2121dgfdg" }};
const request = axios.post(url, dt);
» npm install axios
Front
import React from 'react'import { useState } from 'react'import Sidebar from './sidebar/Sidebar'import Axios from 'axios'import './gui.css'const sideBar3Dark = {height : '100vh',width : '82.5vw',backgroundColor : '#161818',marginLeft: '17.5vw'}function HomeGui() {const [newAboutUs, setVal] = useState("");const handleSubmit = (e) => {e.preventDefault();/*Axios.get('http://localhost:4000/api/get/aboutUs').then(res => {console.log(res);})*/Axios.post('http://localhost:4000/api/post', { Id: newAboutUs }).then(res => console.log(res)).catch(err => console.log(err));
}return (<div className='homeGui'><form onSubmit={ handleSubmit }><input type="text" value = {newAboutUs} onChange={(e) => {setVal(e.target.value)}}/><button></button></form></div>
)}function Gui() {const [contentStyle, setContentStyle] = useState(sideBar3Dark)return (<><div className='gui'><Sidebar changeStyle={(size) => {setContentStyle(size)}} /><div className='mainGui' style={ contentStyle }><HomeGui/></div></div></>
)}export default Gui
Backend
const express = require('express');const app = express();const bodyParser = require("body-parser");const cors = require('cors');const fs = require('fs');app.use(cors());app.use(bodyParser.urlencoded({ extended: true }));app.get("/api/get/aboutUs", (req, res) => {fs.readFile('../client/src/data/aboutUs.json', 'utf-8', (err, jsonString) => {if (err) {console.log(err);} else {try {const data = JSON.parse(jsonString);console.log(data);res.send(data);} catch(err) {console.log(err);}}});});app.post("/api/post", (req, res) => {console.log(req.body.Id);});app.listen(4000, () => {console.log('PORT 4000 connected');});
Essentially at when I perform console.log(req.body.Id) in app.post(...), it just says undefined in my console, but I do not know why.