Pages: [1]   Go Down
Author Topic: POSTing to an Apache Server  (Read 903 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

Is it possible to Post to a server from Arduino?

I am running an Apache server on local host.  I can create a Form in HTML and Post to it from a web page button but I cannot figure out how to Post to the form from my Ethernet Arduino.  There doesn't seem to be a way to invoke the submit function needed by the server.

How is this done?
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

should be posibble, but you might need to write your own code

Posting can be done using HTML post commands, maybe it suports webdav? not sure for apache
but basicly think of it like this if you can press a button on a web page, and that page can retrieve something for you, you can send data to it. you might need to dive into perl on how to achieve this.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should be able to send it like this if you are sending the variables testone and testtwo.
Code:
  client.println("POST /mypage.php HTTP/1.0\r\n\r\ntestone=1&testtwo=2");
Note the double CR/LF. That signifies "end of header". The variables are sent after that, in the body.
Logged

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

Well it doesn't seem to work.

I can format a POST command on my Ethernet Arduino and it does wake up my little PHP script on my Apache server but the $_POST Variable is empty. 
I think its because I have not generated a 'Submit' command the way a submit button does from a web page.

Any other ideas?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well it doesn't seem to work.

I can format a POST command on my Ethernet Arduino and it does wake up my little PHP script on my Apache server but the $_POST Variable is empty. 
I think its because I have not generated a 'Submit' command the way a submit button does from a web page.

Any other ideas?

What request are you actually generating? You could either log it within the webserver, or use WireShark or similar, or print it out of the Arduino. Are you setting the Content-Type and Content-Length headers correctly? Have you tried simply URL encoding the request parameters?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no "Submit" command. How do you know it is waking up the little php script?

edit: Are you using something like this to read the response?
Code:
  client.println("POST /mypage.php HTTP/1.0\r\n\r\ntestone=1&testtwo=2");

  while(client.connected()) {
    while(client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }
  client.stop();


If you want an exact answer, then maybe you should post your code.
« Last Edit: September 02, 2012, 08:59:02 pm by SurferTim » Logged

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9750
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Somebody else's code previously posted in the forum:

Code:
client.println("POST hxxp://yourdomain.com/SMSSite/XMLInterface/Postxml.aspx HTTP/1.0");
// add a header indicating you have content-data to send.  If you set this too low, the server won't see it.  If it is too high, the server may wait, or report an error.
client.print("content-length: ");
client.println(strlen(big_string));
// tell the server you are done with the header.
client.println();
// send the posted data.
client.print(big_string);
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This works. I had to test it first.
Code:
  client.println("POST /arduino.php HTTP/1.0");
  client.println("Content-Type: application/x-www-form-urlencoded");
  // the Content-Length must be set to the number of characters after the double CR/LF
  client.println("Content-Length: 9\r\n");
  client.print("testone=1");
Logged

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

SurferTim and all.  Thank you very much.  This does work.
I have spent several days searching for this answer and you were the only one that came up with the answer.
My lack of knowledge centers around the Headers I expect so I'll go learn about them.
The one thing that you could clear up for me is what you mean by:
// the Content-Length must be set to the number of characters after the double CR/LF

Where/what are the double CR/LF and what is the 9.

Regards........ jim
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the double CR/LF. The client.println is one, and the "\r\n" is the other.
Code:
 client.println("Content-Length: 9\r\n");
This string is nine characters long:
Code:
 client.print("testone=1");
All after the double CR/LF is the content.

edit: I could have sent the double CR/LF like this if it makes it clearer for you
Code:
 client.print("Content-Length: 9\r\n\r\n");

CR/LF = "Carriage Return/Line Feed"
« Last Edit: September 03, 2012, 02:18:43 pm by SurferTim » Logged

Pages: [1]   Go Up
Jump to: