Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:29:05+00:00 2026-06-03T07:29:05+00:00

I wrote some code to periodically post data to a NodeJs program that listens

  • 0

I wrote some code to periodically post data to a NodeJs program that listens for the POST and prints the data. However, sometimes on the header makes it through and sometimes both the header and the body make it through. Here’s the Java code that’s posting the data:

JSONObject dataObject = new JSONObject();
dataObject.put("test", "true");
URLConnection urlConn = thisSubUrl.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput(true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
String dataStr = dataObject.toString();
wr.write(dataStr, 0, dataStr.length());
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
in.close();

The server code looks like this:

var server= http.createServer(function(req,res){
req.setEncoding('utf8');
console.log(req.headers);
req.on('data', function(chunk) {
          console.log("Receive_Event::" + chunk);
         });
req.on('end', function() {
    console.log('on end');
});

console.log("Bytes received: " + req.socket.bytesRead);
if(req.method=='POST'){
    handlePost(req,res);
} else{
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end();
}
}).listen(1338, "127.0.0.1");

The header is always printed out. The number of bytes received varies in length, between the number of the bytes in the header and the number of bytes in the header + the body length.

Why does this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:29:07+00:00Added an answer on June 3, 2026 at 7:29 am

    What is probably happening is that you will emit the res.end() (don’t know what handlePost actually does) before you have received all the “data” chunks. You should call all the extra logic in the “end” event

    var server= http.createServer(function(req,res){
        req.setEncoding('utf8');
    
        console.log(req.headers);
    
        req.on('data', function(chunk) {
            console.log("Receive_Event::" + chunk);
        });
    
        req.on('end', function() {
            console.log('on end');
            console.log("Bytes received: " + req.socket.bytesRead);
            if(req.method=='POST'){
                handlePost(req,res);
            } else{
                res.writeHead(200, {'Content-Type': 'text/plain'});
                res.end();
            }
        });
    
    
    }).listen(1338, "127.0.0.1");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code that works well on chrome. However, when running it on
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
I wrote some code that modifies the images on a webpage. Works with firefox
I wrote some code (about 100 lines) that is working fine on version 5.12.1.
I wrote some code that spits out an image. The code ran on my
I wrote some code to extract data from xls and save them into csv.
I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))
I've wrote some code that will check two dates - they are split into
I wrote some code that stores on the disk some RTP packets with H264
I wrote some code in C for a TCP Server that echoes whatever it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.