1

I've looked at various posts on this topic but am still running into an error with this.

Python code:

import requests
import json

url = 'http://127.0.0.1:8080/ay'
payload = {'some': 'data'}
r = requests.post(url,  data=payload)

print r.text
print r.status_code

Node.js code:

var app = express();
app.use(bodyparser.urlencoded({
    extended: true
}));
app.use(bodyparser.json());

app.post('/ay', function(req, res){
    console.log(req.body);
    res.send('done');
});

So I've looked at my req and even req.body but req.body returns undefined so I think it's with json=payload but I've also tried params=payload and data=json.dumps(payload)

Edit: I forgot to include bodyparser and urlencoded. I edited my code to show the changes.

6
  • Are you using the appropriate form parsing middleware on the server side? Commented Mar 2, 2016 at 4:44
  • What would that be? Do you mean something like express? Commented Mar 2, 2016 at 4:47
  • If you're submitting JSON, you need some kind of middleware that can parse that type of request (e.g. the body-parser module's JSON middleware). Commented Mar 2, 2016 at 4:50
  • Did your codes including body-parser to parse the json request? var bodyParser = require('body-parser'); app.use(bodyParser.json()); Commented Mar 2, 2016 at 5:02
  • I tried that but the body is now {} ... so at least not undefined now. But I'm still not seeing some:data on the node.js side - is it because of the python code now? Commented Mar 2, 2016 at 16:15

1 Answer 1

2

You have to use body-parser to get JSON from request body

var bodyparser = require('body-parser'); 
app.use(bodyparser.json());
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that but the body is now {} ... so at least not undefined now. But I'm still not seeing some:data on the node.js side - is it because of the python code now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.