Python Post Request Body appears empty in Node ser

2019-08-25 08:42发布

I am trying to send a post request via Python to my node.js server

My python code looks like:

payload = {
    'tableName': 'events', 
    'whereParams': {
        'end_date': ['between', yesterday, today]
    }
}

payload = json.dumps(payload)

url = 'http://127.0.0.1:8081/search'
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, headers=headers, data=payload)

jd = r.json()
print jd

My node server file looks like this:

  app = express()
  app.use busboyBodyParser()
  app.use bodyParser.urlencoded({ extended: true })
  app.engine 'html', hogan
  app.set 'view engine', 'html'

  app.post "/search", (req, res, next) ->
    queryController.retrieve req, res, next

and my controller in node looks like this:

exports.retrieve = (req, res, next) ->
  body = req.body
  console.log body

In Node the body is being loged as {} if I print the payload in python it appears correctly as: {"whereParams": {"end_date": ["between", "2015-09-14 00:00:00", "2015-09-15 00:00:00"]}, "tableName": "events"}

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-25 09:29

I found out that I was missing app.use bodyParser.json() from my server code, which seemed to be preventing the server from parsing multipart form data that did not include a file.

查看更多
登录 后发表回答