I'm trying to send a file and other POST variables to a PHP script on my server. There are no good resources on Google and the code samples I've found don't work. Preferably without using cURL.
I'd say roll your own. Its not too complicated.
Capture an HTTP post sent from a browser in Wireshark and reverse engineer as necessary using the spec as your guide. (See Andreas Magnusson's answer below for perhaps more relevant specs.)
I would recommend this approach personally for learning the protocol rather than just going pure spec. Its pretty difficult to learn things just from the spec. I would rather explore the different behaviors by known http clients and try to figure out how things are working by using the spec as my guide.
Format and send the data accordingly over a socket once you're comfortable with HTTP.
Also, If you are not familiar with socket programming, check out Beej's guide to socket programming.
Just a couple of resources make it pretty easy to roll your own
Here is an example of a GET request via ASIO (the C++ networking library in Boost)
Here is the HTTP protocol made really easy
The GET request is how you can view any page on your site. With that code you can download any page and get it as raw text. As you can see it sends a GET header to the server. As explained in that HTTP protocol page, the POST request looks like this
POST /path/script.cgi HTTP/1.0 From:
[email protected] User-Agent:
HTTPTool/1.0 Content-Type:
application/x-www-form-urlencoded
Content-Length: 32
home=Cosby&favorite+flavor=flies
- To send a file:
- You put your URL after post
- change the content type to the type of file you are trying to upload.
- Set Content-Length to the number of bytes in that file
- Append the file after a carrage return (replace "home=Cosby&favorite+flavor=flies")
Another (more quick-n-dirty) solution is to use a utility, via a system() or similar call. For example the wget utility has a --post-file option.
If you're going to roll your own you'd need the relevant RFC for HTTP file uploading (googling on "rfc http file upload" will yield the same result). This RFC also shows how to handle a mix of files and other FORM-data (or POST variables). The problem is of course that you'll probably want to read the MIME RFC as well...