Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send POST data to a webserver?
#1
This is a continuation of a thread at Embarcadero forum, which no longer responds to my input...


Background:
I have programmed an embedded IoT device (ESP8266) for which I have created a configuration application using FPC/Lazarus with Indylaz (Indy 10).
All of this works fine after some help from Remy L.
But there is one thing I also want to incorporate in the config application and this is firmware update via the network of the IoT device.
The ESP8266 has libraries with a lot of functions including web firmware updating, which I have included in my system.
It works as a simple web server operating on the http port of the IoT device, one simply opens the URI http://<ip address of IoT>/firmware and a minimal form is presented where there are two buttons, one file select button to select which firmware file to use and an Update button which starts the firmware update process.

The form presented by the IoT device on the URI shown above has the following html source:
Code:
<html><head><title>The ESP8266 web updater</title></head>
     <body><b>WiFi module firmware updater</b>
     <br>Select firmware file, then click the Update button!<br>
     <form method='POST' action='' enctype='multipart/form-data'>
                  <input type='file' name='update'>
                  <input type='submit' value='Update'>
               </form>
         </body></html>

The code I have used in Lazarus to try and send the data as a web browser would do looks like this after a brief discussion with Remy:
Code:
function TConfigCommHandler.UploadFirmware(FileName: string; URL: string): boolean;
var
  HTTP: TIdHTTP;
  Src: TIdMultipartFormDataStream;
begin
  Result := false;
  if not FileExists(FileName) then exit;
  HTTP := TIdHTTP.Create;
  try
    Src := TIdMultipartFormDataStream.Create;
    try
      Src.AddFile('update', FileName);
      try
        HTTP.Request.Username := FUserName;
        HTTP.Request.Password := FPassword;
        HTTP.Post(URL, Src);
        Result := true;
      except
        on E: Exception do
          FLastError := 'Exception: ' + E.Message;
      end;
    finally
      Src.Free;;
    end;
  finally
    HTTP.Free;
  end;
end;

Behavior:
When I use the form in Firefox and select the file then hit Update, the result is directly shown on the IoT device's debug serial port by a message that it has started the update. Then when the file data has been fully received and verified the IoT device resets and the new firmware starts running. This works fine but involves a web browser...

When I use the method above then there is no error message or such generated, and there is also no debug message from the IoT device saying that update is in progress. In fact once the method is done nothing has happened on the IoT device. It looks like the IoT server is quietly disregarding the call.

So something is clearly missing here, but what?
How does one simulate the action of a form submission in a web browser but using Indy components in a FPC/Lazarus program?
Reply


Messages In This Thread
How to send POST data to a webserver? - by BosseB - 04-30-2018, 09:56 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)