I am having trouble posting a nested JSON object to my node.js server.
Here is what the object should look like:
{
"name" : "Plan 1",
"schedule": [
{
"start": 0,
"days": [
{
"title" : "Day1"
}
]
}
]
}
Here is my server.js code
router.route('/plans')
.post(function(req, res) {
console.log(JSON.stringify(req.body));
var plan = new Plan();
plan.name = req.body.name;
plan.tagline = req.body.tagline;
plan.duration = req.body.duration;
plan.schedule = req.body.schedule;
plan.save(function(err) {
if (err)
res.send(err);
res.send(plan);
});
})
And some python code to make the request. (I have tried using Postman and curl and got the same results)
import requests
payload = {"name" : "Plan 1", "schedule": [{"start": 0, "days": [{"title" : "Day1"}]}]}
r = requests.post("http://localhost:5000/plans", data=payload)
print(r.text)
This is what is output by server.js for the JSON.stringify(req.body)
{"name" : "Plan 1", "schedule" : ["start", "days"]}
And then nothing actually gets saved, so print(r.text)
outputs:
{}
What is the proper way to go about doing this? I am using MongoDB for storage if that is relevant, but it looks like my server.js is not even receiving the full JSON correctly.