Uploading files through a HTTP POST in C++

17,378

Solution 1

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...

Solution 2

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
  1. To send a file:
  2. You put your URL after post
  3. change the content type to the type of file you are trying to upload.
  4. Set Content-Length to the number of bytes in that file
  5. Append the file after a carrage return (replace "home=Cosby&favorite+flavor=flies")

Solution 3

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.

Solution 4

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.

Solution 5

this worked great for me on debian (http get, http post):

http://cpp-netlib.github.com

I use v 0.9.3 that requires boost 1.49

Share:
17,378
Admin
Author by

Admin

Updated on June 26, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Doug T.
    Doug T. almost 15 years
    Ok change "tweak" to "reverse engineer to learn about the protocol". Its pretty difficult to learn from just specs, you need something to play around with.
  • Andreas Magnusson
    Andreas Magnusson almost 15 years
    First of all you need the correct spec (see my answer) and then your initial attempts should always be according to the spec, if that doesn't work, diff:ing with a Wireshark capture from an application that does work is always helpful.