Uploading files via HTTP POST in C ++

I am 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 found do not work. Preferably without using cURL.

+4
source share
5 answers

If you are going to minimize yourself, you will need the appropriate RFC to upload the HTTP files (googling on "rfc http file upload" will give the same result). This RFC also shows how to handle mixed files and other FORM data (or POST variables). The problem, of course, is that you probably want to read the MIME RFC as well ...

+4
source

Only a few resources make it easy to collapse your own

Here is an example of a GET request via ASIO (C ++ network library in Boost)

Here is an HTTP protocol made very easy

A GET request is a way to view any page on your site. With this code, you can load any page and get it as raw text. As you can see, it sends a GET header to the server. As explained in this page of the HTTP protocol , the POST request is as follows

POST /path/script.cgi HTTP/1.0 From: frog@jmarshall.com 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 will post your URL after posting
  • change the type of content to the type of file you are trying to download.
  • Set Content-Length to the number of bytes in this file.
  • Attach the file after returning to the frame (replace "home = Cosby & favorite + flavor = flies")
+2
source

I would say roll. This is not too complicated.

Capturing an HTTP message sent from a browser to Wireshark and reverse engineering if necessary using spec as a guide. (Cm. Andreas Magnusson below is perhaps the more relevant specification.)

I would recommend this approach personally for studying the protocol, and not just for pure specifications. Its pretty hard to learn things only from the specification. I would rather study the different behaviors of well-known http clients and try to figure out how everything works using the specification as my guide.

Format and send data over the socket when you are comfortable with HTTP.

Also, if you are new to socket programming, see the Beej Socket Programming Guide .

+1
source

Another (faster n-dirty) solution is to use the utility through the system () or a similar call. For example, the wget utility has the -post-file option.

+1
source

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

http://cpp-netlib.github.com

I am using v 0.9.3, which requires a boost of 1.49

+1
source

Source: https://habr.com/ru/post/1285930/


All Articles