简体   繁体   English

丢失的 HTTP 请求正文

[英]Lost HTTP request body

I am using Groovy script to perform HTTP POST request with some data:我正在使用 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+"}")
    }
}

This works just fine, Groovy results are below:这工作得很好,Groovy 结果如下:

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

But when I see my web service logs the request body is undefined:但是当我看到我的网络服务日志时,请求正文是未定义的:

在此处输入图片说明

Here's the code:这是代码:

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}...`);
});

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

I'm afraid you've omitted app.use(express.json()) .恐怕你已经省略了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');
});
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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