【问题标题】:Lost HTTP request body丢失的 HTTP 请求正文
【发布时间】:2019-12-12 13:25:11
【问题描述】:

我正在使用 Groovy 脚本对一些数据执行 HTTP POST 请求:

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*

def http = new HTTPBuilder('myhost.com') 
http.request( POST ) {
    uri.path = '/'
    requestContentType = ContentType.JSON
    body =  [title: 'some data', desc: 'some more data']    
    log.info(body.title)
    response.success = { resp,reader ->
        log.info( "POST response status: "+resp.statusLine+"}")
    }
}

这很好用,Groovy 结果如下:

Logs:
INFO : some data
INFO : POST response status: HTTP/1.1 200 OK}

但是当我看到我的网络服务日志时,请求正文是未定义的:

代码如下:

const express = require('express');
const app = express();
var test = {0:'post'};
app.get('/', (req, res) => {
  res.send('a');
  console.log('request inbound');
});

app.post('/',(req,res) => {
    res.send('test');
    console.log('post in');
    console.log(req.body);
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 30000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

我正在使用 Node.js v12.13 | npm v6.12 | express.js 4.17.1

【问题讨论】:

    标签: javascript node.js express http groovy


    【解决方案1】:

    恐怕你漏掉了app.use(express.json())

    const express = require('express');
    const app = express();
    app.use(express.json())
    var test = {0:'post'};
    app.get('/', (req, res) => {
      res.send('a');
      console.log('request inbound');
    });
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2012-04-29
      相关资源
      最近更新 更多