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

Setting up SSL for my localhost with Node.js proved to be a challenging task

I've recently completed the installation of OpenSSL, generated cert.pem and key.pem files, but am encountering issues running my server with node js and express js. It seems like there may be an error in my keys. How can I troubleshoot and resolve thi ...

The node.js API request is experiencing a unresponsive state

As a newcomer to the world of JavaScript, I am currently learning the basics of Node.js. One task I'm working on involves retrieving a contact from my mongoDB and creating a GET method to return it. Sounds simple, right? Below is the router method I ...

Angular offers pre-determined values that cannot be altered, known as "

I am currently learning Angular and TypeScript, and I came across a task where I need to create an object or something similar that allows me to define a readable but not editable attribute. In Java, I would have achieved this by doing the following: publ ...

Obtain the parameters of a function within another function that includes a dynamic generic

I am attempting to extract a specific parameter from the second parameter of a function, which is an object. From this object, I want to access the "onSuccess" function (which is optional but needed when requested), and then retrieve the first dynamic para ...

Display a React functional component

Greetings, friends! I recently created a React app using functional components and now I am looking to print a specific page within the app. Each page is its own functional component, so I was wondering if it's possible to print a component individual ...

Receiving a blank array when sending a piece of information

Currently, I am working on building an API for podcasts using POSTMAN, mongoose, and nodejs. The API consists of Category Name and an Episodes array which includes Episode Number, Episode Name, Episode Description, and a title image. However, when posting ...

Utilizing Next.js with a unique custom Node.js backend implementation

I have successfully developed the frontend using Next.js in order to utilize SSR and enhance SEO. Additionally, I have a personalized NodeJS+express server for managing users and other database tasks (tested thoroughly with Postman). My next step is to in ...

Merge two input fields into one to send data to the backend

I have created two input fields, one for selecting a date and the other for entering a time. Before submitting the form to the backend, I need to combine these two inputs into one variable. For example, <input type="text" name="myDate"> and <input ...

Guide on retrieving a nested JSON array to extract a comprehensive list of values from every parameter within every object

A JSON file with various data points is available: { "success": true, "dataPoints": [{ "count_id": 4, "avg_temperature": 2817, "startTime": "00:00:00", "endTime": "00:19:59.999" }, ... I am trying to extract all the values of & ...

Cannot establish a connection with Socket.IO

I've encountered an issue with establishing a connection to Socket.IO in my node server. Although the server starts successfully with Socket.IO, I am not seeing any console logs when trying to connect to Socket. this.server.listen(this.port, () => ...

Converting an array of objects to an array based on an interface

I'm currently facing an issue with assigning an array of objects to an interface-based array. Here is the current implementation in my item.ts interface: export interface IItem { id: number, text: string, members: any } In the item.component.ts ...

Tips on implementing npm's node-uuid package with TypeScript

Whenever I attempt to utilize node-uuid in TypeScript, I encounter the following issue: Cannot find module uuid This error occurs when I try to import the uuid npm package. Is there a way to successfully import the npm uuid package without encountering ...

Navigating with Angular and NodeJS

I have chosen Node.js for the backend and Angular for the frontend in my project. However, I am curious about what would happen if I were to declare the same route for both the front end and back end. Although I have not tested this scenario yet. For exam ...

Retrieving values from res.send in Jade Template

Having recently delved into the world of node.js and Jade, I've been teaching myself through tutorials on udemy. However, I'm facing some challenges when trying to integrate Jade, Express, and Backbone based on an example. I attempted to use res. ...

What is the process for configuring SSL in node.js and express.js?

I'm currently working on setting up my application to run in HTTPS mode. Despite thinking that my configuration is correct, the page isn't loading at all. Within my SSL folder, I have the following files: credentials.js my-ca.pem my-cert.pem my ...

Exploring the world of Typescript TSX with the power of generic

Introducing JSX syntax support in Typescript opens up new possibilities. I have an expression that functions smoothly with traditional *.ts files, but encounters issues with *.tsx files: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { ...

What could be causing my Javascript prompts to not appear in my Express.IO .ejs file?

I am fairly new to JavaScript and exploring Node with Express.IO. I'm currently working on a project that involves tracking real-time connections made by different 'users' to the server. However, I've encountered an issue where my promp ...

Mastering the Art of Concise Writing: Tips to

Is there a way to write more concisely, maybe even in a single line? this.xxx = smt.filter(item => item.Id === this.smtStatus.ONE); this.yyy = smt.filter(item => item.Id === this.smtStatus.TWO); this.zzz = smt.filter(item => item.Id == ...

Error encountered in React TypeScript: Expected symbol '>' was not found during parsing

While transitioning from JavaScript to TypeScript, I encountered an error in my modified code: Error on Line 26:8: Parsing error: '>' expected import React from "react"; import { Route, Redirect, RouteProps } from "react-router ...

Can one invoke ConfirmationService from a different Service?

How can I declare an application-wide PrimeNG dialog and display it by calling ConfirmationService.confirm() from another service? Below is the HTML code in app.component.html: <p-confirmDialog [key]="mainDialog" class="styleDialog" ...