Overview
Features
Download
Documentation
Community
Add-Ons & Services

HTTP POST help

Please post support and help requests here.

HTTP POST help

Postby justinbanks » 11 Sep 2012, 18:23

I'm attempting to submit a POST request to an http server, and I seem to be missing something fundamental.

In main, I have this snippet:
...
string req = generate_request();
string postdata = generate_postdata(req);
URI uri("http://foo.bar.com/1.1");
std::string path(uri.getPathAndQuery());
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
HTTPResponse response;

do_stuff(session, request, response, postdata);
...


generate_request does nothing more than build a urlencoded xml string that looks like this:
%3Creq%3E%3Cemail%3E%3Crecipient%3E127c08d531eb161d2c02d4404706b623%3C%2Frecipient%3E%3Clist%3E4133%3C%2Flist%3E%3C%2Femail%3E%3C%2Freq%3E

generate_postdata does this:


url = "domain1=" + redir + "&domain2=" + redir;
url += "&logtoken=" + token + "&request=" + request_str + "&test=1";

do_stuff does this:

request.setContentType("application/x-www-form-urlencoded");
session.sendRequest(request) << req;

where the variable req is the output of the generate_postdata() function.

The server doesn't appear to be receiving the post data, and I can't figure out why not.
justinbanks
 
Posts: 2
Joined: 11 Sep 2012, 18:02

Re: HTTP POST help

Postby karas » 13 Sep 2012, 10:13

Everything seems fine, maybe you could try to set the content length. Anyway, the snippet
Code: Select all
HTTPRequest request("POST", "http://www.alepho.com/index.php");
request.setContentType("application/x-www-form-urlencoded");
string httpForm = "do=coding";
request.setContentLength(httpForm.length());
HTTPClientSession session("alepho.com");
session.sendRequest(request) << httpForm;

// print the request headers
for (NameValueCollection::ConstIterator it = request.begin(); it != request.end(); it++)
   cout << it->first << "=" << it->second << endl;
ofstream ofs("header.log");
request.write(ofs);
ofs << "-------------------------------------------------------------------------" << endl;
   
HTTPResponse response;
istream& is = session.receiveResponse(response);
// print the response headers
response.write(ofs);
// print the response body
while (is.good())
{
   char c;
   is.get(c);
   cout << c;
}

works just fine.
karas
 
Posts: 5
Joined: 05 Jul 2009, 11:18
Location: Belgrade

Re: HTTP POST help [SOLVED]

Postby justinbanks » 13 Sep 2012, 18:21

It was indeed the content length that was causing the problem. Thank you.
justinbanks
 
Posts: 2
Joined: 11 Sep 2012, 18:02


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest