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
}
]
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.
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.
--
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
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.
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.