HTML POST Method - How To ?

Blitz3D Forums/Blitz3D Programming/HTML POST Method - How To ?

semar(Posted 2005) [#1]
Hi all,
I'm aware of how to perform an Html GET from within an opened TCP stream - thanks to the examples in the Code Archive.

What puzzles me, is how I can effectively use the POST method, using the same technique, to send a bynary file. What I need, is how the MIME message should be formatted, in order to send it to a remote php script.

I can successfully build an html form to send a file using a POST method; but I don't want to use a form, instead I want to send a file using the same POST method against an opened TCP stream.

Has anybody done already something like that ?

Thanks in advance,
Sergio.


jfk EO-11110(Posted 2005) [#2]
The HTTP 1.1 RFC 2616 may help you, tho it's pretty abstract stuff. Just scroll to point 9.5, POST.
http://www.rfc-editor.org/cgi-bin/rfcdoctype.pl?loc=RFC&letsgo=2616&type=http&file_format=txt

I think I remember I once had a code snippet that is using POST with Blitz, but I can't find it anymore, tried hard without success.

Well, this page may be more useful, scroll to POST and watch the Request Header example:
http://jmarshall.com/easy/http/

Oh yes, and this:
http://www.utoronto.ca/ian/books/html4ed/appb/mimetype.html
http://ppewww.ph.gla.ac.uk/~flavell/www/content-type.html
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html


jfk EO-11110(Posted 2005) [#3]
BTW Semar, I just had this Idea: the easiest way to do this is: write a little HTTP server in Blitz. Then use a form that would upload a file trough a browser. this way you can study a real Post-Header directly. (I confess, I am still kind of confused, how is the form-elements Name transmitted for example?)


GrahamK(Posted 2005) [#4]
Feel free to use my blitz code over at www.squeakyduck.co.uk

It's lurking in the archive section.
Allows HTTP get / post and can be used with proxy servers.


semar(Posted 2005) [#5]
Thanks jfk for the info links - very useful.

@Blitztastic,
that's a nice offer from you; I've downoladed the HTTP get/post bb source from your web site.

I find it interesting; anyway, it's not quite what I need - no offense at all !

I'm trying to build up a simple POST method request, to be sent to an apache web server, from within a blitz application.

So far, I can send a file using a submission form from within IE, but I can't see what really IE sends to the web server. Perhaps I need to build up a self-made http server, and re-direct the IE submission form to it, so I can inspect the header sent.

Anyway.

I've found some interesting documentation about the POST header which is sent from a php script, using a socket; so, using a TCP socket from within Blitz, it should be quite the same:
fwrite($sock, "POST /form_action.php HTTP/1.0\r\n");
fwrite($sock, "Host: secure.example.com\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "$data\r\n");
fwrite($sock, "\r\n");


The key is, now, to translate the above code in the related Blitz syntax.

Write on the socket is trivial, using the Blitz stream commands. But I'm a bit concerned about the 'urlencoded' bit, and how to send the data.

I guess reading the file to transfer, and writing it on the stream. But...

Could anyone enlight me on this purpose ? I just want to upload - via POST method - a small audio file from within a Blitz program to an apache web server.

Thanks for your attention anyway.

Sergio.


fall_x(Posted 2005) [#6]
If you want to urlencode something :

http://www.blitzbasic.com/codearcs/codearcs.php?code=894

But the code you gave above doesn't actually use PHP's urlencode function, it just tells that the content type is urlencoded. I don't think you'll need to urlencode anything if you're only sending a file. If you're sending other data, it may be necessary.


semar(Posted 2005) [#7]
Yes fall_x, you're right, I don't need to urlencode anything; that was only an example of the POST header which I've found while surfing the net.

I just need to know how to build up a simple header to send a file with the POST method from within Blitz; so far, I'm pulling my hair out !!!!

:-/

Sergio.


GrahamK(Posted 2005) [#8]
@Blitztastic,
that's a nice offer from you; I've downoladed the HTTP get/post bb source from your web site.

I find it interesting; anyway, it's not quite what I need - no offense at all !


Not a problem, but I would have thought it would only take a small tweak to the final example to get something working.
I did have a link for the format for file upload, (long time ago when I wrote that library), and it pretty much consisted of adding a few lines to the header.
something along the lines of:
-------------------------<randomnumbers>
some header stuff

file data
-------------------------<randomnumbers>


semar(Posted 2005) [#9]
Again, thank you Blitztastic for your inputs.

The best solution - so far - was to follow the jfk EO-11110 suggestion; so I've build up a simple HTTP server in Blitz, and I've successfully dumped what is sent to a web server, when a POST method is submitted from a form.

And guess what ? In few minutes I've implemented a POST method from within Blitz, and it works like a charm !

Thanks to all for the help,
Sergio.


jfk EO-11110(Posted 2005) [#10]
Please add it to the code archives :) So we all can save our hair and time ;)


jfk EO-11110(Posted 2005) [#11]
just a reminder - semar, pleeease share da code with us.