I had problems doing a POST, so I started playing around with http.request and noticed that it behaves differently from http.get. Why is that?
This is the code I have
var http = require('http'); function callback(response) { var str = ''; response.on('data', function (chunk) { str += chunk; }); response.on('close', function () { console.log(str); }); } function getDataUsingGet() { http.get('http://example.com', callback); } function getDataUsingRequest() { var options = { host: 'example.com', method: 'get' }; http.request(options, callback).end(); }
Calling getDataUsingGet() I get the same reply as I do when I visit using my web browser. But calling getDataUsingRequest() I get
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>501 - Not Implemented</title> </head> <body> <h1>501 - Not Implemented</h1> </body> </html>
And changing method: 'get' to method: 'GET' the reply I receive is
method: 'get'
method: 'GET'
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>400 - Bad Request</title> </head> <body> <h1>400 - Bad Request</h1> </body> </html>
How can I use http.request to get the same page as http.get() gets me? Why do I get different responses when using 'get' and 'GET'?
@Tobbe started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I had problems doing a POST, so I started playing around with http.request and noticed that it behaves differently from http.get. Why is that?
This is the code I have
Calling getDataUsingGet() I get the same reply as I do when I visit using my web browser. But calling getDataUsingRequest() I get
And changing
method: 'get'
tomethod: 'GET'
the reply I receive isHow can I use http.request to get the same page as http.get() gets me? Why do I get different responses when using 'get' and 'GET'?