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.
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...
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:
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 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.
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.
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
Related
I am attempting to send a post request like the one described in the title, but I cannot seem to find a good example of a generic piece of code. My work on my project is a bit tedious as I have never used WinHTTP, only libcurl before which is why I decided to ask here. To reiterate, the http request is a post request with specific header values and json data in the request body which is sent through a proxy to a website using winHTTP. If anyone could provide me with assistance, it would be greatly appreciated.
I am trying to work with basic HTTP using FastCGI and Nginx in c++. I have found the link for fastcgi here: http://chriswu.me/blog/getting-request-uri-and-content-in-c-plus-plus-fcgi/
But there is no clear distinction for HTTP methods like GET and POST. Also, I am unable to figure out how to perform redirection of url using fastcgi. P
I do not have experience with fastCGI and nginx, but since I have used CGI/Apache and took a look of fastCGI samples, I can suggest the following things (and risk to fail with the answer):
GET request is part of URL, so I would parse const char* uri = FCGX_GetParam("REQUEST_URI", request.envp); to check if parameters are given (i.e. if there are key/value pairs after the question mark).
If the previous condition is false, then check if media type in the header is application/x-www-form-urlencoded (meaning it's a POST) and parse HTTP request body to obtain key/value pairs. More info on that can be found at Wikipedia
To perform redirection, use the example but modify response to return HTTP redirection response as described at Wikipedia.
Perhaps fastCGI offers more advanced functions, so all of these can be achieved in a fancy way.
I was going through the MSDN page comparing WinInet and WinHttp. Seems as though WinInet has more functionality than WinHttp. The MSDN page is here. Under what circumstances would one choose WinHttp over WinInet?
Considering that WinInet has HttpSendRequest that can be used to POST data and WinHttp also has WinHttpSendRequest. What would be the advantages of taking WinHttp over WinInet? Is there a difference in how the data is posted using WinHttp and WinInet?
In addition, if some examples of POSTing requests with both WinHttp and WinInet would help, some small sample codes.
EDIT 3 WIRESHARK SCREENSHOT
EDIT 2 I finally managed to get a HTTP_STATUS_CODE from the app and it's 200 OK but the problem comes when sending the post data, it is sent but the parameters aren't set. I tried implementing this code on the PHP end.
<?php
$fp = fopen("data.txt", "a");
fwrite($fp, "ID = " . $_POST['id']);
fclose($fp);
?>
the file is created as soon as the app runs but the ID parameter is not set, it writes to the file
"ID = " and no more. the 10 is not being passed through, no idea why
Thank You.
EDIT: Link to the example I tried to use.
HttpSendRequest to POST form data
Actually MSDN has a good page on WinHTTP vs. WinINet, stating:
With a few exceptions, WinINet is a superset of WinHTTP. When selecting between the two, you should use WinINet, unless you plan to run within a service or service-like process that requires impersonation and session isolation.
This example should help you out and if you are interested in WinInet example you can look in here.
Further more, a quote regarding WinInet from MSDN:
Note WinINet does not support server implementations. In addition, it should not be used from a service. For server implementations or services use Microsoft Windows HTTP Services (WinHTTP).
Which is a good advice.
I am used to consuming Web services via a XMLHttpRequest, to retrieve xml or JSON.
Recently, I have been working with SharePoint REST services, which can return a single value (for example 5532, or "Jeff"). I am wondering if there is a more efficient way than XMLHttpRequest to retrieve this single value. For example, would it work if I loaded the REST url via an iframe, then retrieved the iframe content? Or is there any other well established method?
[Edit] By single value, I really mean that the service just returns these characters. This is not even presented in a JSON or xml response.
Any inefficiency in XMLHttpRequest is largely due to the overhead of HTTP, which the iframe approach is going to incur, as well. Furthermore, if the Sharepoint service expects to speak HTTP, you're going to need to speak HTTP. However, an API does not have to run over HTTP to be RESTful, per Roy Fielding, so if the service provided an API over a raw socket -- or if you simply wanted to craft your own slimmer HTTP request -- you could use a Flash socket via a library like: http://code.google.com/p/javascript-as3-socket/. You could cut the request message size down to under 100 bytes, and could pull out the response data trivially.
The jQuery library is a well established framework which you can use. It´s also an article which answer your concrete question at StackOverflow.
I'm making an HTTP server in c++, I notice that the way apache works is if you request a directory without adding a forward slash at the end, firefox still somehow knows that it's a directory you are requesting (which seems impossible for firefox to do, which is why I'm assuming apache is doing a redirect).
Is that assumption right? Does apache check to see that you are requesting a directory and then does an http redirect to a request with the forward slash? If that is how apache works, how do I implement that in c++? Thanks to anyone who replies.
Determine if the resource represents a directory, if so reply with a:
HTTP/1.X 301 Moved Permanently
Location: URI-including-trailing-slash
Using 301 allows user agents to cache the redirect.
If you wanted to do this, you would:
call stat on the pathname
determine that it is a directory
send the necesssary HTTP response for a redirect
I'm not at all sure that you need to do this. Install the Firefox 'web developer' add-on to see exactly what goes back and forth.
Seriously, this should not be a problem. Suggestions for how to proceed:
Get the source code for Apache and look at what it does
Build a debug build of Apache and step through the code in a debugger in such a case; examine which pieces of code get run.
Install Wireshark (network analysis tool), Live HTTP Headers (Firefox extension) etc, and look at what's happening on the network
Read the relevant RFCs for HTTP - which presumably you should be keeping under your pillow anyway if you're writing a server.
Once you've done those things, it should be obvious how to do it. If you can't do those things, you should not be trying to develop a web server in C++.
The assumption is correct and make sure your response includes a Location header to the URL that allows directory listing and a legal 301/302 first line. It is not a C++ question, it is more of a HTTP protocol question, since you are trying to write a HTTP server, as one of the other posts suggests, read the RFC.
You should install Fiddler and observe the HTTP headers sent by other web servers.
Your question is impossible to answer precisely without more details, but you want to send an HTTP 3xx status code with a Location header.