Difference between http.get and http.request

Posted 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

    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

    <?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'?

  • 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.

  • 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?

  • Yes, pathname, search and query aren't used by http.request or get, 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.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Difference between http.get and http.request

Posted by Avatar for Tobbe @Tobbe

Actions