Why Isn't My NodeJS Post Request Working? (From Django)
Question:
I am trying to send a request that I’m receiving in my Django view to my expressjs api on the same server and I am either getting 500 errors or empty response bodies on the express end.
Here is my express.js code:
var express = require('express');
var fs = require('file-saver');
var app = express();
var multer = require('multer');
var upload = multer({ dest: 'json/' });
const router = express.Router();
const bodyParser = require('body-parser');
//app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/example', function (req, res) {
res.send('Hello World!');
});
app.post('/example', async (req, res) => {
await fs.saveAs(req.body, 'json.json');
await console.log('Got body:', req.body);
await res.sendStatus(200);
});
app.listen('8080', function () {
console.log('Test');
});
Here is my django view:
from django.http import HttpResponse
from django.core.mail import EmailMessage, send_mail
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.http import FileResponse
from rest_framework.decorators import api_view
import json
from reportsappapi.utilities import logip
import requests
@method_decorator(csrf_exempt, name='dispatch')
@api_view(['GET', 'POST'])
def sendMail(request):
url = 'https://example.com/example'
clockout = request.FILES['clockout']
clockoutJSON = json.load(clockout)
x = request.post(url, clockout)
return HttpResponse(status=204)
Answers:
It should be
x = requests.post(url, clockout)
just like Arun T has said.
The request in use is the current server incoming request object and not the python request library.
You can as well use import alias, for example.
import requests as fetch
And then
x = fetch.post(url, clockout)`
To prevent common name errors
I am trying to send a request that I’m receiving in my Django view to my expressjs api on the same server and I am either getting 500 errors or empty response bodies on the express end.
Here is my express.js code:
var express = require('express');
var fs = require('file-saver');
var app = express();
var multer = require('multer');
var upload = multer({ dest: 'json/' });
const router = express.Router();
const bodyParser = require('body-parser');
//app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/example', function (req, res) {
res.send('Hello World!');
});
app.post('/example', async (req, res) => {
await fs.saveAs(req.body, 'json.json');
await console.log('Got body:', req.body);
await res.sendStatus(200);
});
app.listen('8080', function () {
console.log('Test');
});
Here is my django view:
from django.http import HttpResponse
from django.core.mail import EmailMessage, send_mail
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.http import FileResponse
from rest_framework.decorators import api_view
import json
from reportsappapi.utilities import logip
import requests
@method_decorator(csrf_exempt, name='dispatch')
@api_view(['GET', 'POST'])
def sendMail(request):
url = 'https://example.com/example'
clockout = request.FILES['clockout']
clockoutJSON = json.load(clockout)
x = request.post(url, clockout)
return HttpResponse(status=204)
It should be
x = requests.post(url, clockout)
just like Arun T has said.
The request in use is the current server incoming request object and not the python request library.
You can as well use import alias, for example.
import requests as fetch
And then
x = fetch.post(url, clockout)`
To prevent common name errors