http request

Posted on
  • Hello!
    I'm trying to parse xml from url. My code looks like:

    var wifi = require("Wifi");
    function getFeed(){
          var options = {
                method: 'GET',
                host: "http://www.upwork.com",
                port: 443,
                path: "/ab/feed/jobs/rss",
                protocol:"https"
          };
         var contents = "";
         var req = require('http').request(options, function(res) {
                console.log(res);
                res.on('data', function(data) {
                      contents += data;
                      console.log(contents);
                });
                res.on('close', function() {
                    console.log('CLOSE');
                    contents = "";
                });
      });
      req.on('error', function(e) {
                console.log('Error');
                console.log(e);
       });
      req.end();
    }
    wifi.connect("ssid", {password:"pass"}, function(err){ 
      if (err) {
        console.log("Connection error: "+err);
        return;
      }
      getFeed();
      console.log("connected? err=", err, "info=", wifi.getIP());
    });
    

    And I got response:

    httpCRs {
      "headers": {
        "Server": "cloudflare-nginx",
        "Date": "Mon, 25 Dec 2017 18:01:36 GMT",
        "Content-Type": "text/html",
        "Content-Length": "177",
        "Connection": "close",
        "CF-RAY": "-"
       },
      "httpVersion": "1.1",
      "statusCode": "400",
      "statusMessage": "Bad Request"
     }
    <html>
    <head><title>400 Bad Request</title></head>
    <body bgcolor="white">
    <center><h1>400 Bad Requ
    <html>
    <head><title>400 Bad Request</title></head>
    <body bgcolor="white">
    <center><h1>400 Bad Request</h1></center>
    <hr><center>cloudflare-nginx</center>
    </body>
    </html>
    CLOSE
    

    Seems espruino tries to get host by it's IP, but as I know target site is not accessible by it's IP. Typing url

    https://www.upwork.com/ab/feed/jobs/rss

    in browser's address line will return rss feed

    So my question is there any workaround for that?

    Would appreciate any help

  • Hey!

    It appears the host parameter might be the problem, try stripping the http:// from there:

    host: "http://www.upwork.com",
    

    (oops perhaps that is just this forum problem that it tries to convert all hostnames to a link? In the edit field of this post there is no "http://" in the host field value)

  • thanks for your interest. I've tried without http. this didn't help

  • And now try with a colon in the protocol. Like protocol:"https:". I have tried this in the Linux Espruino build and it worked for me.

  • Thanks. Just tried. Got response:

    <html>
    <head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
    <body bgcolor=
    <html>
    <head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
    <body bgcolor="white">
    <center><h1>400 Bad Request</h1></center>
    <center>The plain HTTP request was sent to HTTPS port</center>
    <hr><center>cloudflare-nginx</center>
    </body>
    </html>
    

    Did you get xml in response?

  • With options like

    var options = {
                method: 'GET',
                host: "http://www.upwork.com",
                path: "/ab/feed/jobs/rss",
        protocol:"https"
            };
    

    I'm not setting port. And host also without "http" part. Response look follow:

    httpCRs {
      "headers": {
        "Date": "Tue, 26 Dec 2017 14:45:42 GMT",
        "Transfer-Encoding": "chunked",
        "Connection": "close",
        "Cache-Control": "max-age=3600",
        "Expires": "Tue, 26 Dec 2017 15:45:42 GMT",
        "Location": "https://www.upwork.com/ab/feed/jobs/rssĀ­",
        "X-Content-Type-Options": "nosniff",
        "Server": "cloudflare-nginx",
        "CF-RAY": "3d34d34e07fe6409-FRA"
       },
      "httpVersion": "1.1",
      "statusCode": "301",
      "statusMessage": "Moved Permanently"
     }
    0
    CLOSE
    
  • The issue seems to be that the protocol you're using needs HTTPS, but it isn't supported in Espruino for ESP8266. Instead, you'd need an Espruino WiFi or ESP32.

  • Got it. Thanks

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

http request

Posted by Avatar for tempos @tempos

Actions