0

I have this Java code

DefaultHttpClient httpclient = new DefaultHttpClient();         
        HttpPost httpPostRequest = new HttpPost(URL);           
        StringEntity se;            
        se = new StringEntity(jsonObjSend.toString());          
        // Set HTTP parameters          
        httpPostRequest.setEntity(se);          
        httpPostRequest.setHeader("Accept", "application/json");            
        httpPostRequest.setHeader("Content-type", "application/json");          
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); 
        // only set this parameter if you would like to use gzip compression            
        long t = System.currentTimeMillis();            
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 

and this in node.js

var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Entering");


if ( request.method === 'POST' ) {

     // the body of the POST is JSON payload.
     request.pipe(process.stdout);   
     }

});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Im using the pipe to write everything in the console to be sure that i receive the data. What i actually want is to parse the data back to JSON and then save it in a array. How do i get the data from the request? Does anyone have an code example?

Thanks

2 Answers 2

1
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Entering");


if ( request.method === 'POST' ) {

        // the body of the POST is JSON payload.
        request.pipe(process.stdout);   

        var data = '';
        request.on('data', function(chunk) {
            data += chunk;
        });

        request.on('end', function() {
            try {
                data = JSON.parse(data);
            } catch (e) {
                console.log(e);
            }
        });
    }

});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot.. I have a last question to the line "request.on('data', function(chunk)...." What does the parameter 'data' mean... Is it defined somewhere?
'data' and 'end' are event codes. check this docs nodejs.org/api/http.html#http_event_data
0

try to use the following concept in your code

        response.on('data', function (chunk)
        {
                var data = chunk.toString();
                var data_val = JSON.parse(data)
          });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.