Pages: [1]   Go Down
Author Topic: How to format a Get Request  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Near San Diego
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an external server running MySQL. I have installed a php script that, when accessed from HTTP will deliver data from one of the tables. It works fine from a browser:
Code:
http://www.seti.net/php/getEvents.php
I cannot figure out how to send this command from Arduino. I have the EthernetClient library working and can access Google as in the example.

When I send this command through the client:
Code:
client.println("//GET /php/getEvents.php HTTP/1.0");
the server returns:
Code:
</head><body>
<h1>Method Not Implemented</h1>
<p>GET to /php/getEvents.php not supported.<br />
What is the correct way to format a GET in Arduino?
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use a GET to send a temperature to my web server. Note the lack of HTTP version and the additional blank line after the get - not sure whether the difference is important but it works:

Code:
void  SendTempToServer()
{
char GetRequest[80];
if (client.connect(server, 80))
  {
  sprintf(GetRequest,"GET /cgi-bin/Arduino.cgi?Reading=%s&arduino_id=23&sensor_id=1",Temperature);
  //Serial.println(GetRequest);
  client.println(GetRequest);
  client.println();
  }
}
Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 144
Posts: 5981
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do not use the double slash preceding the GET.
Code:
// change this
client.println("//GET /php/getEvents.php HTTP/1.0");
// to this
client.println("GET /php/getEvents.php HTTP/1.0");

And your web host uses virtual hosting. Insure you send the Host parameter. I use HTTP/1.1 for that.
Code:
client.println("GET /php/getEvents.php HTTP/1.1");
client.println("Host: www.seti.net");
client.println("Connection: close\r\n");
Logged

Near San Diego
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool - That does work.  Thank you very much.
One last.  Where can I go to learn why it works?  Do you have a good reference or a Google subject I can read?
Thanks again.
Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 144
Posts: 5981
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The correct format on the GET line is important for obvious reasons. The virtual server is a bit more obscure. Here is a wiki article on virtual hosting.
http://en.wikipedia.org/wiki/Virtual_hosting
A Google search works good for that if you need more info.
Logged

Near San Diego
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool again surfer.  It looks like I have to go learn about HTTP Headers - Right?
Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 144
Posts: 5981
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right, and the ability to recognize a virtual server so you know what header parameters are required.  smiley

Here is how I determined your server uses virtual hosting. In the address bar of your browser
http://www.seti.net
and you get your home page.

In nslookup
Code:
C:\Documents and Settings\user>nslookup www.seti.net
Server:  ip68-105-28-16.at.at.cox.net
Address:  68.105.28.16

Non-authoritative answer:
Name:    seti.net
Address:  205.134.250.170
Aliases:  www.seti.net

Then try that ip in your web browser
http://205.134.250.170
You will get your web hosting service page
inmotionhosting.com
Logged

Near San Diego
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Surfer - learning a lot but one last and then I'll go away and study.
I also have on that same server a php script that sets the database.  It can be called from a browser like this:
Code:
http://www.seti.net/php/setEvent.php?pixel_ID=1212
What is the equivalent code for the arduino to do this?
« Last Edit: June 22, 2013, 09:46:22 am by James-Brown » Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 144
Posts: 5981
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
client.println("GET /php/getEvents.php?pixel_ID=1212 HTTP/1.1");
client.println("Host: www.seti.net");
client.println("Connection: close\r\n");

Here is my client code.
http://playground.arduino.cc/Code/WebClient
I use a character array to pass the page and those parameters like this:
Code:
    sprintf(pageAdd,"/php/getEvents.php?pixel_ID=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
Logged

Near San Diego
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sweet. smiley-cool
Logged

Pages: [1]   Go Up
Jump to: