Go Down

Topic: Heroku Request (Read 491 times) previous topic - next topic

jonarnett90

I am attempting to grab some information off of a Heroku app I set up, however I am hit with Heroku's 404 page. I tested a similar request with a simple Python script, which worked fine.

The Arduino Code:
Code: [Select]

if (!client.connected()) {
    Serial.println("connecting...");
   
    if (client.connect(serverName, 80)) {
      Serial.println("connected");
     
      // Make a HTTP request
      client.println("GET / HTTP/1.0");
      client.println();
     
      while (!client.available());
     
      // If data can be read from te server, print it
      while (client.available()) {
        char c = client.read();
        Serial.print(c);
      }
     
      Serial.println("Done");
     
      client.stop();
    } else {
      // If you didn't get a connection to the server:
      Serial.println("connection failed");
    }
  }


For the record, as this is different from the example, you may question whether the altered algorithm works. It does, the exact same thing sending a request to Google works fine.

Python Code:
Code: [Select]

import httplib

h = httplib.HTTPConnection("ruby-coffee-maker.herokuapp.com")

h.request("GET", "/")

r = h.getresponse()
data = r.read()

print r.status, r.reason, "\"" + data + "\""

h.close()


Thanks in advance for any help!

billroy

It is necessary to send a Host header so Heroku knows which app to route your request to:

From http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html:
Code: [Select]

       GET /pub/WWW/ HTTP/1.1
       Host: www.w3.org


-br

PaulS

Code: [Select]
      client.println("GET / HTTP/1.0");
You are asking for an unnamed script at the root of the unnamed server to be executed.

Without knowing what server you are pointing the request to, all we can surmise is that you are doing it wrong.

jonarnett90

@billroy: I tried replacing "GET /" with "GET /pub/WWW/", to no avail. Also, Heroku does not support HTTP 1.1.

@PaulS: My bad, I left that bit out. Here you are:

Code: [Select]

char serverName[] = "ruby-coffee-maker.herokuapp.com";


I was a bit concerned that "-" was an illegal character, but changing it on the server side to not have hyphens yielded a net gain of nada.

billroy

1. That was an example from the link I provided.  It is necessary to adapt it to your code.

2. You need a Host: header.  Did you try that?


-br

jonarnett90

@billroy: I tried a few Host: headers, which still yielded 404s. What would the header code look like for this case?

billroy

Did you consider trying the Heroku application hostname you are attempting to connect to?

-br

jonarnett90

@ billroy: I tried it to no avail.

Go Up