Single file upload example using Indy TidHTTP and multipart/form-data

A new cross-language example for HTTP and REST is now available on GitHub.

Description

Single file upload example using Delphi client and Java server code and HTTP multipart/form-data

Requirements

  • Delphi 2009 or newer
  • Indy 10.6.2
  • Java JDK 8 or newer
  • Apache Maven
  • WildFly application server

Client

program IndyPostFormData;

{$APPTYPE CONSOLE}

uses
  IdHTTP, IdMultipartFormData, SysUtils;

const
  URL = 'http://localhost:8080/indy-post-formdata-1.0-SNAPSHOT/webresources/generic/pdf';

var
  Indy: TIdHTTP;
  Params: TIdMultiPartFormDataStream;
  Response: string;

begin
  Indy := TIdHTTP.Create;
  Params :=  TIdMultiPartFormDataStream.Create;
  try
    try
      Params.AddFile('file', 'client.pdf');
      Response := Indy.Post(URL, Params);
      WriteLn(Response);
    except
      on E:Exception do
        Writeln(E.Classname, ': ', E.Message);
    end;
  finally
    Params.Free;
    Indy.Free;
  end;
  ReadLn;
end.

 

Server (main REST method)

  @POST
  @Path("/pdf")
  @Consumes({MediaType.MULTIPART_FORM_DATA})
  public Response upload(MultipartFormDataInput input) {
    String UPLOAD_PATH = "c:/tmp/";
    try {
      InputStream fileInputStream = input.getFormDataPart("file", InputStream.class, null);
      String fileName = "test.pdf";

      int read;
      byte[] bytes = new byte[1024];

      try (OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileName))) {
        while ((read = fileInputStream.read(bytes)) != -1) {
          out.write(bytes, 0, read);
        }
      }
    } catch (IOException e) {
      throw new WebApplicationException("Error while uploading file. Please try again");
    }
    return Response.ok("Data uploaded successfully").build();
  }

 

Full source code

https://github.com/michaelJustin/indy-post-formdata

Advertisements

Daraja Framework 1.2.2 released

Habarisoft released Daraja Framework 1.2.2, a maintenance release which fixes bugs in the BootstrapDemo demo code and optional units (ShutDownHelper). The new release also includes an example project for a HTTPS server, not included are the certificates and the OpenSSL libraries.

bildschirmfoto-am-2017-01-27-um-19-23-13-fullpage

Daraja Framework unit test enhancements

The Daraja Framework unit tests (DUnit/FPCUnit based) are now more self-documenting. A new HTTPTestCase test base class provides methods CheckGETResponseEquals, CheckPOSTResponseEquals, CheckGETResponseContains, CheckGETResponse404, and others, which allow to write more concise unit test methods.
The changes are available in the master branch, a snapshot is available for download here.

Continue reading “Daraja Framework unit test enhancements”

Daraja Framework: commercial license

Daraja Framework is a HTTP service framework for Free Pascal and Delphi 2009 and newer.

Its source code is dual licensed under the GNU Affero General Public License Version 3 and a commercial license, which removes the requirements of the AGPL license. When the annual subscription is terminated, the license is still valid for unlimited time. Termination of the subscription only ends additional services such as the free (basic) support.

Continue reading “Daraja Framework: commercial license”

Daraja Framework RESTful extension 2.1 released

Daraja Framework is a free open source library for Object Pascal, based on the stand-alone HTTP server in Internet Direct (Indy).

In the past, the separate “experimental” RESTful extension library only compiled with Delphi, as it used anonymous methods.

With version 2.0 / 2.1, the RESTful extension library now also compiles with Free Pascal, where it uses procedural types instead of anonymous methods.

In projects which must be single-source for Delphi and Free Pascal, usage of procedural types can be enforced by defining the symbol DARAJA_RESTFUL_COMPATIBLE.

 

Continue reading “Daraja Framework RESTful extension 2.1 released”