Intel Galileo Azure REST call

70 Views Asked by At

i'm trying to make use of my Intel Galileo Gen2 by making calls to a microsoft web api (REST service) hosted in azure as an web app but all i get is an azure 404 saying it won't find my app

the code on my arduino is the following

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "wack.azurewebsites.net";

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /api/warning HTTP/1.0");
    client.println();
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

i tried changing the target url several times

 client.println("GET wack.azurewebsites.net/api/warning HTTP/1.0");
 client.println("GET api/warning HTTP/1.0");
 client.println("GET /api/warning ");
 client.println("GET /api/warning HTTP/1.1");

none of them work, some don't even connect however it only returns a 404 even though that URL is valid and tested throughout differnet applications even on the same network.

i have replaced the mac adress with the adress of the device and previously have confirmed that it can be reached in the network by making it a web server itself. Basically it can grab internet settings but for some reason will not connect to my rest service. Any kind of help is appreciated. Thx

2

There are 2 best solutions below

4
Marcu Marian On

The problem was not providing a host for the call. I come from a c# background and thought by initializing the HttpClient with the server address would be enough but apparently you have to provide this line

client.println("GET /api/warning HTTP/1.1");
client.println("Host: wack.azurewebsites.net");
client.println("User-Agent: Intel Galileo");
client.println("Connection: close\r\n");

each time you make a request

0
evilSnobu On

You're missing the Host header.

The Host header is mandated by HTTP/1.1 protocol specification, you MUST send it with every request.

You should also send User-Agent and Connection: close to be a good HTTP citizen. The latter tells the web server "Don't keep the socket open since I'll hang up right away". HTTP/1.1 defaults to Connection: keep-alive.

Amend your request to look like this:

client.println("GET /api/warning HTTP/1.1");
client.println("Host: wack.azurewebsites.net");
client.println("User-Agent: Intel Fakelileo");
client.println("Connection: close\r\n");

A simple test:

$ telnet {sitename}.azurewebsites.net 80                              

Connected to waws-prod-am2-119.cloudapp.net.
Escape character is '^]'.
GET / HTTP/1.1
Host: {sitename}.azurewebsites.net
User-agent: Intel Fakelileo
Connection: close


HTTP/1.1 200 OK
Content-Length: 84
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.0
...