1

I'm using the starter kit by @ErikRas

With the following code, I'm having trouble authenticating my python program.

Here's my python:

import requests
URL="http://localhost"
PORT="3030"

Session = requests.Session()
Request = Session.post(URL+':'+PORT+'/login', data={'name':'AuthedUserName'})
# (Password to follow proof of concept obviously)

In my api.js file i just had:

import express from 'express';
import session from 'express-session';
import bodyParser from 'body-parser';
import config from '../src/config';
import * as actions from './actions/index';
import {mapUrl} from 'utils/url.js';
import http from 'http';

const app = express();

const server = new http.Server(app);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
  secret: 'react and redux rule!!!!',
  resave: false,
  saveUninitialized: false,
  cookie: { maxAge: 60000 }
}));

app.use((req, res) => {
/* There's heaps here, but all that is relevant is: */
console.log(req.body)

In the console i'm just getting {}

I found this article: req.body empty on posts and Python Post Request Body appears empty in Node server when sent

but as you can see i'm already using bodyparser.json and bodyparser.urlencoded.extended = true

Community
  • 1
  • 1
  • Are you getting 200 from the Python side? Type `Request.raise_for_status()` after the post line. – Burhan Khalid Mar 21 '16 at 06:53
  • I was getting 404, but that's because node was getting req.body.user == 'undefined' so validation was falling flat. But now that i've got headers={}, i'm getting 200 –  Mar 21 '16 at 07:11

2 Answers2

2

Okay, so i compared my pythons request against my web-app's request by printing the request to the console in node.

I found that the web app had more in its header than the python's requests' request. WebApp: referer: 'http://localhost:3001/login' origin: 'http://localhost:3001' host: 'http://localhost:3001' connection: 'close'

So I included this in my header, it worked!

I wanted to see which header property was 'necessary', so i gradually pulled everything out to see if this broke the POST request.

Turns out i managed to pull everything out! So what i'm using now is this:

r = Session.post(URL+':'+PORT+'/login',headers = {}, data={'name':'AuthedUserName'})

That's it!! I'd like to understand why headers={} works, but i need to moving forward with my project!!

<<<<<<---- Edit ---->>>>>>

Above is 'half' right, since my web app is using json and i want to use json, what i needed to do was change my header to

headers = {u'content-type': u'application/json'}

Then use json.dumps on the payload only!

r = session.post('http://'+DB_URL+':3030/sinumecUpdate', headers = headers, data = json.dumps(dataObject))

I also needed to pull out

app.use(bodyParser.urlencoded({ extended: true }));

From my node API and stick with only the JSON body parser.

1
import requests
import json

url = 'http://127.0.0.1'
data={'name':'AuthedUserName'}
headers = {'content-type': 'application/json'}

r = requests.post(url=url, data=json.dumps(data), headers=headers)
CyberMage
  • 51
  • 5
  • 5
    Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer could benefit from an explanation. – Trenton McKinney Sep 07 '19 at 22:10