Express encounters difficulty in processing Chunked Post Data

I am currently retrieving data from a Campbell Scientific data logger. This data is being posted to an application that is coded in Typescript using Express and BodyParser. The request successfully reaches the app (as I'm able to debug it), however, the body/query appears as Object{}.

To ensure that the data logger is indeed sending data, I created a simple PHP program that dumps the post data along with its 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);
}

The output of this script is:

[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

  • It seems that there is a line break in the JSON after '"data": [' which may be causing the issue. Could it be related to the transfer-encoding type?

This suggests that there might be an error in my Typescript application.

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);

In the same application, I gather information from another data logger that sends data through a GET request, and it works perfectly fine with the same code. I am unsure if I need to handle this particular data logger differently (perhaps with special options in BodyParser), because when I debug the Typescript app, I can only see the headers while the body, parameters, raw, query... appear empty (Object {}).

https://i.stack.imgur.com/NRCYI.png

Thank you!

Answer №1

Exciting announcement!

I have found the solution. To address chunked requests (and the line break in the PHP code), it is crucial to implement its corresponding 'on' events:

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;
        // HANDLE THE DATA
        response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(200).send('ALL CLEAR');
    });

    request.on('error', function (error){
        response.set('Cache-Control', 'no-store, no-cache, must-revalidate, private').status(400).send('ERROR OCCURRED');
    });
}

Many thanks!!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a Node.js module function within the main routes file

I am facing an issue in my main routes file where I need to execute a piece of code multiple times. To avoid repetition, I decided to create a separate module called 'getDayWorkout' and call it whenever needed. However, I am encountering difficul ...

Mastering the Art of Importing JS Modules in a Nodejs-Express Environment

Currently, I am in the process of learning how to utilize nodejs with express as my framework and handlebar as the template engine. Note: This information is taken from the chartjs tutorial where they employ parcel. However, when attempting to replicate ...

Leveraging dotenv path within JEST

I've been attempting to incorporate a different .env file specifically for my Jest tests, but unfortunately, I haven't been successful in getting it to function properly. package.json: { "name": "task-manager", "version": "1.0.0", "descri ...

Issue with dynamic imports and lazy-loading module loadChildren in Jhipster on Angular 8, not functioning as expected

When utilizing dynamic import, it is necessary to modify the tsconfig.json file in order to specify the target module as esnext. ./src/main/webapp/app/app-routing.module.ts 14:40 Module parse failed: Unexpected token (14:40) File was processed with these ...

Issue encountered when trying to use Array.sort() method to sort an array of objects

I'm facing an issue sorting an array of objects by a name property present on each object. When utilizing the sort() method with the given code snippet, I encounter the following error: ERROR ReferenceError: b is not defined This is my code block: m ...

Error encountered in Typescript: The property 'prevUrl' is expected to be present in the props object, but it appears to be missing

When trying to access the props.prevUrl property, the following error is encountered: Property 'prevUrl' does not exist on type '{ nextUrl: string; } | { prevUrl: string; nextUrl: string; } | { prevUrl: string; confirm: () => void; }&apos ...

What could be causing the issue of todo items not displaying correctly in the specified React component? (Using ASP.NET Core 2.0 with ReactJS and TypeScript)

Hello everyone, I am seeking some assistance and guidance on a React/Typescript issue I am facing with my new demo app. As a beginner in React/Typescript, I am using Visual Studio 2017 Community with asp.net core 2.0 and the react boiler project template. ...

Inferring types from synchronous versus asynchronous parameters

My objective is to create an "execute" method that can deliver either a synchronous or an asynchronous result based on certain conditions: type Callback = (...args: Arguments) => Result const result: Result = execute(callback: Callback, args: Arguments) ...

The declaration file for the module 'tailwind-scrollbar' could not be located

Currently, I am in the process of utilizing Tailwind packages for a Next.js application, however, I have encountered an issue that has proved to be quite challenging to resolve. Every time I attempt to add a "require" statement to my tailwind.config.js fil ...

Exploring nested objects within an instance

I'm facing an issue with accessing a variable object within my main object. I am able to access 'start', 'end', and 'category' without any problem, but I am unsure how to access the variable Object in my Angular web app d ...

JavaScript's ASYNC forEach function not following the expected sequence

I'm really struggling to understand the workings of async and await in this scenario. I want the forEach function to run before the console.log and res.json, but no matter what I do with async and await, it always ends up being the last thing executed ...

The process of automatically formatting Typescript has transformed into an unfortunate auto-discarding action

Typescript autoformatting has become a concerning issue. Whenever I input quoted strings (" or `), the code surrounding it seems to temporarily glitch, with other strings appearing as code. This problem has recently escalated, particularly with strings li ...

What is the syntax for declaring a function type with an optional parameter in Typescript?

I have a function with optional parameters that I am passing down to another component. execute = (option: string = 'default'): void => { // ... } In the receiving component, there is a property called executeFunction where I intend to assi ...

What is the best way to identify errors in an express listen callback function?

My current code is set up to verify if there was an error while initiating Express: express() .listen(port, (err: Error) => { if (err) { console.error(err); return; } console.log(`Express started`); ...

Troubleshooting import errors with Typescript for C3 and D3 libraries

I have recently started working on a project using the C3 graphing library within an Ionic2/Angular2 TypeScript setup. After installing C3 via npm and the type definitions via tsd, I imported it into my own TypeScript file like this: import {Component} fr ...

Typescript encounters a failure in typing when an object is destructured

There is a function that returns an object with two properties (res, mes) where one of them could be null: const fetchJSON = <Res, Body>(link: string, body: Body): Promise<{ res: Res; mes: null } | { res: null; mes: Popup }> => { return n ...

Invoke an ActionCreator within a different ActionCreator

Calling an ActionCreator from another file is proving to be a challenge... The products.ts file contains the ActionCreators and Reducers for Products... import { setStock } from './Store.ts'; //.... export const addProduct = (product: IProduct) ...

What is the best approach to have a method in the parent class identify the type based on a method in the child class using TypeScript?

I'm faced with a code snippet that looks like this. class Base{ private getData(): Data | undefined{ return undefined } public get output(): Data | undefined { return { data: this.getData() } } } class ...

The proper way to utilize the Mongoose save method within an Express POST route

Having recently delved into Express and Node, I encountered an issue that has left me puzzled. I have established two separate Express routes to handle POST requests for distinct Mongoose data models. One route functions correctly and returns as expected, ...

Difficulty encountered while managing dropdown functionality in Protractor using TypeScript

I'm encountering some difficulties when it comes to selecting a dropdown in Protractor. Here's the structure of my DOM: https://i.stack.imgur.com/qK8sT.png This is the XPath I'm using to select the dropdown with the value "Yes": //label[ ...