简体   繁体   中英

Can't seem to POST using Python and Node.js

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.

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

var bodyparser = require('body-parser'); 
app.use(bodyparser.json());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM