Is my web service the problem?

I’m struggling with the TCPClient. So I thought it would be a good idea to check out other projects like Should I Bring an Umbrella

So I uploaded @sebnil’s code to my spark and connected with putty. I was able to see that it was raining in Sweden!

This part is from @sebnil’s code:

Serial.println("connecting...");
if (client.connect("sebastiannilsson.com", 80)) {
  Serial.println("connected");
  client.println("GET /will-it-rain/ HTTP/1.0");
  client.println("Host: sebastiannilsson.com");
  client.println();
} 

I built myself a web service that returns two color codes according to the weather at my place:
http://roman-mueller.ch/api/weather/

so I just edited your code on line 18 and following to this:

Serial.println("connecting...");
if (client.connect("roman-mueller.ch", 80)) {
  Serial.println("connected");
  client.println("GET /api/weather/ HTTP/1.0");
  client.println();
}

I uploaded it again to the core but this time it somehow messed it up

what i see in putty:

disconnecting.
connecting...
connected

but then nothing more. what i hear is the sound windows makes when you unplug and replug a usb device.
so it looks like the tcpclient call to my service crashes the core somehow?!
but how can this be? all I changed was the url… and the web service seems ok!

could somebody please confirm this?

I see that for:

http://sebastiannilsson.com/will-it-rain/ the result is:

rain: 1

http://roman-mueller.ch/api/weather/ the result is

"W-[000,100,000][144,238,144] - Temp: 10,0°C; Color [DarkGreen] - Condition: FewClouds; Color [LightGreen]"

The outputs are different so the remaining code might not behave correctly.

Yes this is true your result is different from sebnil result.

ok, i was not too specific i guess.

Yes I know, that my web service is supposed to return something else than @sebnil’s.

The point is, I don’t want to run my version of “should i bring an umbrella”.
My problem is that I cannot access my web service with a TCPClient. So I was looking around for code where it apparently works. This is why I reused @sebnil’s code.

So when I use that code but with my url I would expect that I would see something like

"W-[000,100,000][144,238,144] - Temp: 10,0°C; Color [DarkGreen] - Condition: FewClouds; Color [LightGreen]"

in putty.

Instead I only see

disconnecting.
connecting...
connected

followed from window’s USB device connection drop / reconnect.

So the problem is:

Why does

Serial.println("connecting...");
if (client.connect("roman-mueller.ch", 80)) {
  Serial.println("connected");
  client.println("GET /api/weather/ HTTP/1.0");
  client.println();
}

not work?

You should try putting back the:

Serial.println(“connecting…”);
if (client.connect(“roman-mueller.ch”, 80)) {
Serial.println(“connected”);
client.println(“GET /api/weather/ HTTP/1.0”);
client.println();
}

And see if there is an output. Then we can specifically pinpoint is it’s code or webservice issue

this is EXACTLY what I am doing

Hi @kwyjib0

I think you need a “Host: roman-mueller.ch” line in your TCP request. Try adding a client.println() for that.

When I do a reverse lookup on 80.190.254.165, I get a host name of server60.hostfactory.ch. With the advent of edge routers that cache web pages at the edge of networks and multi-name hosting with one physical server being known by many names, the Host: field in the HTTP GET request has become just about required.

Think of it this way–there is one HTTP server on 80.190.254.165 even though that host can serve for many different aliased host names so the Host: field in the request is the only way the HTTP server can know what content to serve.

@bko Agree with what you mentioned. I noticed that missing line so i ask if @kwyjib0 could use back the original without that line and see if it worked.

Here is the function that I use with good results:

void sendGetRequest(const char * server, const char * url)
{
    if (myTCP.connect(server, 80)) {
        //Serial1.print("Connected to Server");
        myTCP.print("GET ");
        myTCP.print(url);
        myTCP.println(" HTTP/1.0");
        myTCP.println("Connection: close");
        myTCP.print("Host: ");
        myTCP.println(server);
        myTCP.println("Accept: text/html, text/plain");
        myTCP.println();
        myTCP.flush();
    } 
}

I think the key part is the Host: part.

I think your IIS server is being particularly persnickety. For one thing, yes, you need to include the ‘Host’ header. And for another, your server is returning a 404 (Page not found) error when I use HTTP 1.0, but works fine if I specify HTTP 1.1. So, give this a try:

Serial.println("connecting...");
if (client.connect("roman-mueller.ch", 80)) {
  Serial.println("connected");
  client.println("GET /api/weather/ HTTP/1.1");
  client.println("Host: roman-mueller.ch");
  client.println("Connection: close");
  client.println();
}

Technically, the ‘Host’ header is an HTTP 1.1 thing, IIRC. But I’ve never encountered a server before that didn’t honor it even when the GET request specified HTTP 1.0. There may be some setting for this buried somewhere in your server config?