-
• #2
Try:
var options = { host: 'example.com', path: '/', method: 'GET' }; http.request(options, callback).end();
I think the issue is that the path isn't specified, so it's asking the server for a page called
""
, which it complains about.If you used
var options = url.parse('http://example.com')
it'd neatly populate everything you need for you.Why do I get different responses when using 'get' and 'GET'?
Because that string is being sent to the HTTP server as-is, and the HTTP server is case-sensitive. Espruino doesn't convert it to upper-case for you just in case you're trying to do something non-standard with an HTTP server.
I'll add something to the docs on it.
-
• #3
Yes, please do update the docs :) The whole options object could use some documentation improvements :)
Just noticed that for http.request it says
options An object containing host,port,path,method,headers fields
And for url.parse it says
An object containing options for http.request or http.get. Contains method, host, path, pathname, search, port and query
So url.parse returns a bunch of options (pathname, search, query) not mentioned as supported in http.request (or http.get)
Could you please try to clarify that?
-
• #4
Yes,
pathname
,search
andquery
aren't used byhttp.request
orget
, but they're useful if you're making your own web server and want to parse out the arguments.There are a bunch of examples under the
Examples
heading that show how you'd use it.
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'?