Created
October 23, 2013 23:51
-
-
Save gustavo-rodrigues-dev/7128892 to your computer and use it in GitHub Desktop.
Não consigo navegar entre as sessões com o node, por exemplo, preciso acessar a url principal, para capturar os dados de cookie para ai sim fazer uma segunda requisição (autenticada), esse problema foi facilmente resolvido usando cURL com PHP, gostaria de saber se existe alguma auternativa para esse tipo de coisa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Não Funciona */ | |
var http = require('http'); | |
var options = { | |
host: 'www.olhovivo.sptrans.com.br', | |
port: 80 | |
}; | |
http.get(options, function(resp){ | |
var teste = resp.headers; | |
teste = teste['set-cookie'][0].split('='); | |
var cok = 'apiCredentials-v0='+teste[1].replace('; path', ''); | |
resp.on('data', function(chunk){ | |
//new request?? | |
var options2 = { | |
host: 'www.olhovivo.sptrans.com.br', | |
port: 80, | |
"set-cookie": cok, | |
path: '/v0/Parada/Buscar?termosBusca=Paulista' | |
}; | |
http.get(options2, function(resp2){ | |
resp2.on('data', function(chunk2){ | |
//new request?? | |
http.createServer(function (req, res) { | |
//set content header | |
res.writeHead(200, { | |
'content-type': 'application/json' | |
}); | |
//write msg | |
res.end(chunk2); | |
}).listen(8080); | |
console.log('Server running 0n 8080'); | |
}); | |
}).on("error", function(e){ | |
console.log("Got error: " + e.message); | |
}); | |
}); | |
}).on("error", function(e){ | |
console.log("Got error: " + e.message); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'http://olhovivo.sptrans.com.br/'); | |
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
$store = curl_exec ($ch); | |
curl_setopt($ch, CURLOPT_URL, 'http://olhovivo.sptrans.com.br/v0/Parada/Buscar?termosBusca=paulista'); | |
$content = curl_exec ($ch); | |
echo $content; | |
curl_close ($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Express.js para facilitar seu routing..
ou de uma olhada nesse link http://stackoverflow.com/questions/6084360/node-js-as-a-simple-web-server