Express 无法读取 Chunked Post 数据
Express can't read Chunked Post Data
我正在从 Campbell 科学数据记录器收集数据。此数据记录器 post 信息到使用 Express 和 BodyParser 在 Typescript 中编码的应用程序。请求获取应用程序(能够对其进行调试),但 body/query 是并且 Object{}
为了确保数据记录器发送数据,我做了另一个程序,一个简单的 PHP 代码,它转储 post 数据,它是 headers:
$json_params = file_get_contents("php://input");
$log->lwrite($json_params);
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
$log->lwrite('header:'.$header.' Value:'.$value);
}
结果是:
[26/Aug/2019:17:10:59] (test - 12452) {"head": {"transaction": 0,"signature": 11786,"environment": {"station_name": "idtest","table_name": "Tabla1","model": "CR300","serial_no": "1646","os_version": "CR310.Std.08.01","prog_name": "CPU:19014_Badajoz_Inundacion_Dev1.CR300"},"fields": [{"name": "Voltage","type": "xsd:float","units": "Volts","process": "Avg","settable": false},{"name": "Temp","type": "xsd:float","units": "Grados C","process": "Avg","settable": false},{"name": "HR","type": "xsd:float","units": "%HR","process": "Smp","settable": false},{"name": "Lluvia","type": "xsd:float","units": "mm","process": "Tot","settable": false},{"name": "Distancia","type": "xsd:float","units": "cm","process": "Avg","settable": false},{"name": "Calado","type": "xsd:float","units": "cm","process": "Avg","settable": false}]},"data": [
{"time": "2019-08-26T17:09:00","no": 0,"vals": [12.26,"NAN","NAN",0,"NAN","NAN"]}]}
`[26/Aug/2019:17:10:59] (test - 12452) header:User-Agent Value:CR310.Std.08.01`
`[26/Aug/2019:17:10:59] (test - 12452) header:Host Value:192.168.1.33`
[26/Aug/2019:17:10:59] (test - 12452) header:Transfer-Encoding Value:chunked
- 请注意,在“"data": [”之后的 json 中有一个换行符,这可能是一个问题?还是 transfer-encoding 类型?
这意味着我在我的 typescript 应用程序中做错了。
this.app = express();
this.port = port;
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.use(bodyParser.json());
this.router = express.Router();
this.router.post(this.path_campbellStation
this.updateCampbellStationPost);
在同一个应用程序中,我从另一个数据记录器收集信息,该数据记录器发送数据但发送 GET 请求并且工作正常(相同的代码)。
我不知道我是否必须以其他方式处理它(具有特殊选项的 BodyParser),因为当我调试打字稿应用程序时,我只能读取 header 而 body、参数、原始, query... 为空 (Object {})
谢谢!
大家好消息!
我得到了解决方案。处理分块请求的方法(因此 PHP 代码中的线路制动器)正在实现其 'on' 事件:
updateCampbellStationPost = (request: express.Request, response: express.Response) => {
var data: string = '';
const _this = this;
request.on('data', function(chunk) {
data += chunk;
});
request.on('end', function() {
request.body = JSON.parse(data);
const _data = data;
// DEAL WITH THE DATA
response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(200).send('IS ALL RIGHT');
});
request.on('error', function (error){
response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(400).send('IS FUCKED');
});
}
谢谢!!
我正在从 Campbell 科学数据记录器收集数据。此数据记录器 post 信息到使用 Express 和 BodyParser 在 Typescript 中编码的应用程序。请求获取应用程序(能够对其进行调试),但 body/query 是并且 Object{}
为了确保数据记录器发送数据,我做了另一个程序,一个简单的 PHP 代码,它转储 post 数据,它是 headers:
$json_params = file_get_contents("php://input");
$log->lwrite($json_params);
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
$log->lwrite('header:'.$header.' Value:'.$value);
}
结果是:
[26/Aug/2019:17:10:59] (test - 12452) {"head": {"transaction": 0,"signature": 11786,"environment": {"station_name": "idtest","table_name": "Tabla1","model": "CR300","serial_no": "1646","os_version": "CR310.Std.08.01","prog_name": "CPU:19014_Badajoz_Inundacion_Dev1.CR300"},"fields": [{"name": "Voltage","type": "xsd:float","units": "Volts","process": "Avg","settable": false},{"name": "Temp","type": "xsd:float","units": "Grados C","process": "Avg","settable": false},{"name": "HR","type": "xsd:float","units": "%HR","process": "Smp","settable": false},{"name": "Lluvia","type": "xsd:float","units": "mm","process": "Tot","settable": false},{"name": "Distancia","type": "xsd:float","units": "cm","process": "Avg","settable": false},{"name": "Calado","type": "xsd:float","units": "cm","process": "Avg","settable": false}]},"data": [
{"time": "2019-08-26T17:09:00","no": 0,"vals": [12.26,"NAN","NAN",0,"NAN","NAN"]}]}
`[26/Aug/2019:17:10:59] (test - 12452) header:User-Agent Value:CR310.Std.08.01`
`[26/Aug/2019:17:10:59] (test - 12452) header:Host Value:192.168.1.33`
[26/Aug/2019:17:10:59] (test - 12452) header:Transfer-Encoding Value:chunked
- 请注意,在“"data": [”之后的 json 中有一个换行符,这可能是一个问题?还是 transfer-encoding 类型?
这意味着我在我的 typescript 应用程序中做错了。
this.app = express();
this.port = port;
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.use(bodyParser.json());
this.router = express.Router();
this.router.post(this.path_campbellStation
this.updateCampbellStationPost);
在同一个应用程序中,我从另一个数据记录器收集信息,该数据记录器发送数据但发送 GET 请求并且工作正常(相同的代码)。 我不知道我是否必须以其他方式处理它(具有特殊选项的 BodyParser),因为当我调试打字稿应用程序时,我只能读取 header 而 body、参数、原始, query... 为空 (Object {})
谢谢!
大家好消息!
我得到了解决方案。处理分块请求的方法(因此 PHP 代码中的线路制动器)正在实现其 'on' 事件:
updateCampbellStationPost = (request: express.Request, response: express.Response) => {
var data: string = '';
const _this = this;
request.on('data', function(chunk) {
data += chunk;
});
request.on('end', function() {
request.body = JSON.parse(data);
const _data = data;
// DEAL WITH THE DATA
response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(200).send('IS ALL RIGHT');
});
request.on('error', function (error){
response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(400).send('IS FUCKED');
});
}
谢谢!!