[fpc-pascal] Prorammatically sending file using HTTP POST?

Anthony Walter sysrpl at gmail.com
Wed Apr 4 19:21:03 CEST 2018


HTTP is quite simple actually. You just build a post message (a string),
and send it over a socket. I've implemented HTTP client functions in many
computer languages. Here are the key take aways if you want to implement it
yourself.

Build this string filling out the blanks using your own values

POST {location} HTTP 1.1{linefeed}
Host: {domain}{linefeed}
Content-Length: {length}{linefeed}
{linefeed}
{content}

So to post the word 'hello' to http://example.com/yourpage?say, where the
domain is example.com, and location is yourpage?say, the string would look
like this in pascal.

Message :=
  'POST /yourpage?say HTTP 1.1'#10 +
  'Host: example.com'#10 +
  'Content-Length: 5'#10 +
  #10 +
  'hello';

Then you use some socket component, set the Host to 'example.com' so that
it can resolve the ip address, and call Send(Message).

This assumes you have a tcp client socket component with properties Host,
and a method called Send, but most any/every socket component does.

If you want to post form named arguments in content, then just format them
like:

Content := 'words=hello&priority=high&account=123';
ContentLength := IntToStr(Length(Content));

Message :=
  'POST /yourpage?say HTTP 1.1'#10 +
  'Host: example.com'#10 +
  'Content-Length: ' + ContentLength + #10 +
  #10 +
  Content;

Socket.Host := 'example.com';
Socket.Send(Message);

It's that easy.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180404/1b0fd90c/attachment.html>


More information about the fpc-pascal mailing list