I am trying to send a post request from python to my nodeJS server. I can successfully do it from client-side js to nodeJS server using the fetch API but how can I achieve this with python? What I tried below is sending the post request successfully but the data/body attached to it is not reaching the server. What am I doing wrong and how can I fix it? Thanks in advance.
NOTE: All my nodeJS routes are set up correctly and work fines!
//index.js
'use strict';
const express = require('express')
const app = express()
const PORT = 5000
app.use('/js', express.static(__dirname + '/public/js'))
app.use('/css', express.static(__dirname + '/public/css'))
app.set('view engine', 'ejs')
app.set('views', './views')
app.use(cookie())
app.use(express.json({
limit: '50mb'
}));
app.use('/', require('./routes/pages'))
app.use('/api', require('./controllers/auth'))
app.listen(PORT, '127.0.0.1', function(err) {
if (err) console.log("Error in server setup")
console.log("Server listening on Port", '127.0.0.1', PORT);
})
//server file
//served on http://127.0.0.1:5000/api/server
const distribution = async(req, res) => {
//prints an empty object
console.log(req.body)
}
module.exports = distribution;
//auth
const express = require('express')
const server = require('./server')
const router = express.Router()
router.post('/server', server)
module.exports = router;
//routes
const express = require('express')
const router = express.Router()
router.get('/', loggedIn, (req, res) => {
res.render('test', {
status: 'no',
user: 'nothing'
})
})
#python3
import requests
API_ENDPOINT = "http://127.0.0.1:5000/api/server"
data = '{"test": "testing"}'
response = requests.post(url = API_ENDPOINT, data = data)
print(response)