2016-03-21 48 views
1

我使用@ErikRas节点快速清空体使用Python API POST(请求库)

用下面的代码,我无法验证我的Python程序的入门套件时。

这里是我的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) 

在我api.js文件我只是有:

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) 

在控制台我刚{}

我发现这篇文章: req.body empty on posts and Python Post Request Body appears empty in Node server when sent

,但你可以看到我已经使用bodyparser.json和bodyparser.urlencoded.extended =真

+0

你从Python方面获得200?在帖子后输入'Request.raise_for_status()'。 –

+0

我得到了404,但那是因为节点获得了req.body.user =='undefined',所以验证一直在持平。但现在我已经得到了标题= {},我得到了200 – Brad

回答

0

好了,所以我比较我的蟒蛇通过打印请求到控制台要求对我的web应用程序的请求节点。

我发现web应用程序的标题比python的请求更多。 Web应用程序: 引用者: 'http://localhost:3001/login' 产地: 'http://localhost:3001' 主持人: 'http://localhost:3001' 连接: '关闭'

所以我在报头包含在此,它的工作!

我想看看哪个头属性是'必要的',所以我逐渐拉出所有东西,看看这是否打破了POST请求。

原来我设法把所有东西都拉出来!所以我现在使用的是:

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

就是这样!我想了解为什么标题= {}有效,但我需要继续我的项目!

< < < < < < ---- ----编辑>>>>>>

以上是 '半壁江山' 的权利,因为我的web应用程序是使用JSON和我想使用JSON,我需要做的是改变我的头

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

然后使用json.dumps上只有有效载荷!

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

我还需要拉出

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

从我的节点API和只用JSON解析器身体棒。