Go Down

Topic: POSTing to an Apache Server (Read 930 times) previous topic - next topic

James-Brown

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?

PGT

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.

SurferTim

You should be able to send it like this if you are sending the variables testone and testtwo.
Code: [Select]
  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.

James-Brown

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?

PeterH


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?
I only provide help via the forum - please do not contact me for private consultancy.

SurferTim

#5
Sep 03, 2012, 03:55 am Last Edit: Sep 03, 2012, 03:59 am by SurferTim Reason: 1
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: [Select]
  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.

zoomkat


Somebody else's code previously posted in the forum:

Code: [Select]

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);
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

SurferTim

This works. I had to test it first.
Code: [Select]
  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");


James-Brown

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

SurferTim

#9
Sep 03, 2012, 09:06 pm Last Edit: Sep 03, 2012, 09:18 pm by SurferTim Reason: 1
This is the double CR/LF. The client.println is one, and the "\r\n" is the other.
Code: [Select]
 client.println("Content-Length: 9\r\n");
This string is nine characters long:
Code: [Select]
 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: [Select]
 client.print("Content-Length: 9\r\n\r\n");


CR/LF = "Carriage Return/Line Feed"

Go Up