Undefined data from GET request

660 views
Skip to first unread message

newtriks

unread,
Nov 25, 2011, 5:07:09 PM11/25/11
to nodejs
Hi,

I have been going round in circles on this one and was wondering if
someone may help shed some light on where I may be going wrong? I have
a simple GET request which when I run using curl sends back the
following json data:

curl http://121.1.1.12:7999/json/lrd

lrd
[
{
"resource_name":"lrd",
"state":0
}
]

In my node app I run the following:

var request = my_client.request('GET', '/json/lrd',
{'host': '121.1.1.12'});
request.end();
request.on('response', function (response) {
var data = "";
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on('data', function (chunk) {
data+=chunk;
});

response.on('end', function () {
console.log(data.length);
});
});

Which outputs:

STATUS: 200
HEADERS: {"content-type":"text/html"}
0

So I basically cannot grab the data being sent within the nodejs app,
debugging in the browser shows the data and as does curl as shown
above?

Google debug:

GET http://121.1.1.12:7999/json/lrd HTTP/1.1
Host: 121.1.1.12:7999
Proxy-Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/
535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Response Headers
HTTP/1.1 200 OK
Content-Type: application/json
Proxy-Connection: Close
Response
[
{
"resource_name":"lrd",
"state":0
}
]

mscdex

unread,
Nov 25, 2011, 6:31:44 PM11/25/11
to nodejs
On Nov 25, 5:07 pm, newtriks <newtr...@gmail.com> wrote:
> In my node app I run the following:
>
> var request = my_client.request('GET', '/json/lrd',
> {'host': '121.1.1.12'});

I believe you're missing the correct port number (7999). By default
it's using port 80.

Also on an unrelated note, it seems you're using a really old version
of node if my_client is not an instance of a third party module.

mscdex

unread,
Nov 25, 2011, 6:34:36 PM11/25/11
to nodejs
On Nov 25, 6:31 pm, mscdex <msc...@gmail.com> wrote:
> I believe you're missing the correct port number (7999). By default
> it's using port 80.

Oops, forgot that with node 0.2.x, the actual hostname and port are
given during a separate line with .createClient and not .request.
Anyway, I'd still check that you're supplying the right port
with .createClient.

Simon Bailey

unread,
Nov 25, 2011, 6:35:43 PM11/25/11
to nod...@googlegroups.com
@Martin thanks I have tried that but get the same results. It would appear the 'on' data handler is not firing at all hence no data when it comes to the 'end' handler running?

@mscdex I am using v0.6.2?


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Simon Bailey

unread,
Nov 25, 2011, 6:36:54 PM11/25/11
to nod...@googlegroups.com
Oh and I am communicating with an Arduino configured on port 7999, will that make a difference to nodejs?

mscdex

unread,
Nov 25, 2011, 6:52:33 PM11/25/11
to nodejs
On Nov 25, 6:35 pm, Simon Bailey <newtr...@gmail.com> wrote:
> @mscdex I am using v0.6.2?

Oh ok. Just for giggles, what if you instead try:

require('http').get({
host: '121.1.1.12',
port: 7999,
path: '/json/lrd'
}, function(res) {
var data = "";
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data', function (chunk) {
console.log('got chunk length: ' + chunk.length);
data += chunk;
});
res.on('end', function () {
console.log('final data length: ' + data.length);
});
}).on('error', function(err) {
console.log('error: ' + err.message);
});

On Nov 25, 6:36 pm, Simon Bailey <newtr...@gmail.com> wrote:
> Oh and I am communicating with an Arduino configured on port 7999, will
> that make a difference to nodejs?

It shouldn't, as long as the remote http server isn't acting funky in
some way.

Simon Bailey

unread,
Nov 25, 2011, 7:03:19 PM11/25/11
to nod...@googlegroups.com
:) nice but no cigar. I am wondering if I should go back and rework my response code on the Ardiuno webserver....? Its just odd that curl and browsers render the data?

Simon Bailey

unread,
Nov 25, 2011, 7:04:48 PM11/25/11
to nod...@googlegroups.com
That data handler still doesn't fire?

STATUS: 200
HEADERS: {"content-type":"application/json"}
final data length: 0

mscdex

unread,
Nov 25, 2011, 7:59:46 PM11/25/11
to nodejs
On Nov 25, 7:04 pm, Simon Bailey <newtr...@gmail.com> wrote:
> That data handler still doesn't fire?
>
> STATUS: 200
> HEADERS: {"content-type":"application/json"}
> final data length: 0

Well, at least you're getting the right content-type header from the
server now :-)

I'm not aware of any issues with http client requests.... but maybe
you could run tcpdump or wireshark to capture your client request and
see if there is indeed (json) data being sent in the response.

Simon Bailey

unread,
Nov 26, 2011, 6:44:40 AM11/26/11
to nod...@googlegroups.com
Hey both, 

Thanks for all the input. I went back to the original construction of the data within the Arduino program and swapped it out for some new code to construct the JSON data. Boom, it works :) So it would appear that the GET response was missing something which nodejs didn't like.

Just for anyone else's reference if your gonna setup an Arduino as a webserver and handle GET/POST etc requests with node.js then this library works a charm https://github.com/sirleech/Webduino

Cheers all,

Simon

Reply all
Reply to author
Forward
0 new messages