7

I'm trying to get html page through this node module called Wreck

It should be very easy to get data but I'm unable to get them

'use strict';

var Wreck = require('wreck');

var url = 'http://www.google.it';

var callback = function(err, response, payload){
  Wreck.read(response, null, function(err, body){
      //here print out the html page
  });
};

Wreck.get(url, callback);

Here above a simple script just a copy from the readme of the developer. according to the documentation body should return a buffer object but how can I read inside a body object? I have read to use toJSON or toString() but I don't get any result

1
  • 1
    Can you show us what exact code you did use in the read callback, and what it printed? Did an error happen? Commented Aug 29, 2014 at 14:14

1 Answer 1

10

...but I don't get any result

You ARE getting a result, an empty Buffer, but it's not want you want, probably.

The fact is: you are using the read method wrong, passing it inside a callback to the get method. The methods get, post, put and delete already call read internaly and return the readable Buffer for you, in a callback. Take a look at the get doc:

get(uri, [options], callback)

Convenience method for GET operations.

  • uri - The URI of the requested resource.
  • options - Optional config object containing settings for both request and read operations.
  • callback - The callback function using the signature function (err, response, payload) where:
    • err - Any error that may have occurred during handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

So, the use of the get method is pretty straightforward (using your own example):

var callback = function(err, response, payload){
  console.log(payload.toString()); // converting the buffer to a string and logging
};

Wreck.get(url, callback);
Sign up to request clarification or add additional context in comments.

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.