9

My goal is to upload a text file via HTTP post I am using Delphi 2009.

Say for example to the following URL

https://www.example.com/ex/exampleAPI.asmx/Process

I understand it can be done by using the TIdHttp component. And the following call

IdHttp1.Post();

But I cant figure out how to set up everything i.e. specifying the url and including the file to be posted.

Thanks.

mjn
  • 36,362
  • 28
  • 176
  • 378
Trevor
  • 16,080
  • 9
  • 52
  • 83

1 Answers1

11

TIdHTTP has two overloaded versions of Post() that take a filename as input:

var
  Response: String;

Response := IdHTTP1.Post('https://www.example.com/ex/exampleAPI.asmx/Process', 'c:\filename.txt');

.

var
  Response: TStream;

Response := TMemoryStream.Create;
IdHTTP1.Post('https://www.example.com/ex/exampleAPI.asmx/Process', 'c:\filename.txt', Response);
...
Response.Free;

Note that you are posting to an HTTPS URL, so you need to first assign an SSL-enabled IOHandler, such as TIdSSLIOHandlerSocketOpenSSL, to the TIdHTTP.IOHandler property beforehand.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you, but when I try to compile I get the following error. "E2250 There is no overloaded version of 'Post' that can be called with these arguments" any ideas? – Trevor Jun 05 '12 at 14:33
  • 1
    Then you are not using an up-to-date version of Indy, because the current version does have such overloads available: `function Post(AURL: string; const ASourceFile: String{$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}): string; overload;` and `procedure Post(AURL: string; const ASourceFile: String; AResponseContent: TStream); overload;` – Remy Lebeau Jun 05 '12 at 17:48
  • Alright I'm trying to update Indy, but its a pain I am having a lot of trouble. I have never installed packages before.. Is there a way to do it with a TWebBrowser by chance? – Trevor Jun 05 '12 at 20:36
  • To do it with a `TWebBrowser`, you have to store the entire file data into a `Variant` that contain a `varByte` array (use `VarArrayCreate()` to allocate that `Variant`, then you can copy the file data into its memory block), then you can pass the `Variant` to the `PostData` parameter of the `TWebBrowser.Navigate()` method. `TWebBrowser` is a visual component, though. It is not meant to be used for non-visual work. You could switch to Microsoft's `WinInet/WinHTTP` API instead, though. – Remy Lebeau Jun 05 '12 at 21:45
  • Hey so I was able to get the Indy components updated. But I am getting an error for both cases of TIdHTTP post. For the first case you mentioned I get a "HTTP/1.1 500 Internal Server Error." - Do you have an idea of what that might mean? And for the second case I get "Incompatible types: 'TStream' and 'procedure, untyped pointer or untyped parameter'" apparently its not able to handle my Response : TStream; parameter.. – Trevor Jun 06 '12 at 19:26
  • A `500` reply means the server encountered an error on its end. Typically, that happens if the HTTP request is malformed. As for the compiler error, I can't answer that without seeing the code you are using now. – Remy Lebeau Jun 06 '12 at 19:44
  • var Response: TStream; | begin | Response := TMemoryStream.Create; | Response := IdHTTP1.Post('http://handybilling.com/bf/BillflashAPI.asmx/Process?action=au&username=demo&password=demo','C:\BILLFLASH\BILLFLASH_STATEMENT2.txt',Response);| Response.Free; - Thats all I have I don't know exactly how to pull back a response yet, but it wont let me compile at this point. Just gives me the above mentioned error. – Trevor Jun 06 '12 at 20:06
  • IdHTTP1.Post(' http://handybilling.com /bf/BillflashAPI.asmx/Process?action=au&username=demo&password=demo','C:\BILLFLASH\BILLFLASH_STATEMENT2.txt',Response); It was not showing up very well on my last comment so I put a couple of spaces to break up the url. – Trevor Jun 06 '12 at 20:14
  • `Post()` can **EITHER** return a `String` **OR** fill a `TStream`, but you are trying to do both. You need to pick one or the other. As for code not showing in your comments, use [formatting markup](http://meta.stackoverflow.com/editing-help#comment-formatting). – Remy Lebeau Jun 06 '12 at 21:20