Description
The httpPOSTStart()
– function establishes a connection to a listening TCP server (e.g. a webserver) and starts a http POST request. The application can then either send a file (from SerialFlash) or arbitrary data to the server. httpPOSTEnd()
finishes the upload and returns the servers response / ‘Content-Length’. The application can now read the response data from the client object.
The function httpPOSTCreateBoundary()
helps in cases, that the POST should contain more than one element.
Syntax
client.httpPOSTStart( os_server, os_path, os_ElementName, ou32_size, os_filename, ou16_port
);
client.httpPOSTCreateBoundary( os_ElementName );
client.httpPOSTEnd();
Parameters
String os_server: a string specifying the host part of the URL (e.g. “di-ce.de”)
String os_path: a string of the path and file of the upload PHP script on the server (e.g. “/cgi/upload.php”)
String os_ElementName: a string naming the element to be posted
uint32_t ou32_size: the size of the data to be posted to the server (e.g. file.size()) or the size of the total arbitrary data including the boundary strings.
optional:
– String os_filename: in case a file should be uploaded (posted), otherwise use: “”
– uint16_t ou16_port: TCP port of the request (=80 if omitted)
Returns
return of httpPOSTStart()
:
int:
0 – error no connection could be established
1 – OK. Connection is established and the POST header was send to the server. The application can now send the actual data to the server.
return of httpPOSTEnd()
:
int:
0 – connection error
<0 – http error code of the webserver (e.g. -400 = Bad request)
>0 – ‘Content-Length’ of the body. That is the number of bytes to be expected for the following read() calls.
return of httpPOSTCreateBoundary()
:
String:
add the returned string at the beginning of the new element within the POST body. This is only necessary if there are more than one element in the POST. If there is only one element, then the element naming is done by the Start and End functions.
Example Code
The following example can be used on any DICE because it makes use of the HTTP_Client type: CANIP_Client
. If you like to use the functions on a different technology, replace it by any of the following #includes & classes: WiFiHTTPClient, EthernetHTTPClient or GSMHTTPClient
#include "CANIP.h"
void setup()
{
String s_server = "proddat.di-ce.de";
String s_path = "/postReceive.php";
CANIP_Client t_postClient(&MCAN0, &BOOTLOADER_SETTING);
while(!Serial);
Serial.println("STARTUP");
String s_post = BOOTLOADER_SETTING.s_get_sernNo();
if(!t_postClient.httpPOSTStart(s_server, s_path, String("SerNr"), s_post.length()))
{
Serial.println("Could not connect to the server");
while(1){ yield(); };
}
t_postClient.print(s_post);
if(t_postClient.httpPOSTEnd() >= 0)
{
while(t_postClient.available() || t_postClient.connected())
{
if(t_postClient.available())
Serial.write(t_postClient.read());
}
}
else
Serial.println("Error");
Serial.println("\n -- connection closed --");
t_postClient.stop();
}
void loop()
{}
Notes and Warnings
Use the function httpPOSTCreateBoundary()
and add it to the POST-String only in cases where 2 or more elements should be posted in one request. (see examples in the Arduino IDE)
There is also an example PHP script for the server side. Find it in the sketch folder of the examples.
Examples can be found in the Arduino IDE menu:
File -> Examples -> DICE -> HTTP_Client -> POST
See also
Find more details here: