bugraarslan
bugraarslan

Reputation: 153

C++ Boost Asio Post Request to HTTP | PHP

I am trying to write a function that sends post request to a HTTP server, php file.

    int post(string url, string data, string & content){
        try{
            content = "";
            if(substr_count(url, "/") == 0){
                url += "/";
            }
            string host = substr(url, 0, strpos(url, "/"));
            string path = substr(url, strpos(url, "/"));
            boost::asio::io_service io_service;
            tcp::resolver resolver(io_service);
            tcp::resolver::query query(host, "http");
            tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
            tcp::resolver::iterator end;
            tcp::socket socket(io_service);
            boost::system::error_code error = boost::asio::error::host_not_found;
            while(error && endpoint_iterator != end){
                socket.close();
                socket.connect(*endpoint_iterator++, error);
            }
            if(error){
                throw boost::system::system_error(error);
            }
            boost::asio::streambuf request;
            std::ostream request_stream(&request);
            request_stream << "POST " << path << " HTTP/1.0\r\n";
            request_stream << "Host: " << host << "\r\n";
            request_stream << "User-Agent: C/1.0";
            request_stream << "Content-Type: application/x-www-form-urlencoded\r\n";
            request_stream << "Accept: */*\r\n";
            request_stream << "Content Length: " << data.length() << "\r\n";
            request_stream << "Connection: close\r\n\r\n";
            request_stream << data;
            boost::asio::write(socket, request);
            boost::asio::streambuf response;
            boost::asio::read_until(socket, response, "\r\n");
            istream response_stream(&response);
            string http_version;
            response_stream >> http_version;
            unsigned int status_code;
            response_stream >> status_code;
            string status_message;
            getline(response_stream, status_message);
            if(!response_stream || http_version.substr(0, 5) != "HTTP/"){
                cerr << "Invalid response\n";
                return 1;
            }
            if(status_code != 200){
                cerr << "Response returned with status code " << status_code << "\n";
                return 1;
            }
            boost::asio::read_until(socket, response, "\r\n\r\n");
            string header;
            while(getline(response_stream, header) && header != "\r"){}
            while(true){
                size_t n = boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error);
                if(!error){
                    if(n){
                        stringstream ss;
                        ss << &response;
                        content += ss.str();
                        last_connection_ip_address = socket.remote_endpoint().address().to_string();
                    }
                }
                if(error == boost::asio::error::eof){
                    break;
                }
                if(error){
                    throw boost::system::system_error(error);
                }
            }
            return 1;
        }catch(exception & e){
            cerr << "Exception: " << e.what() << "\n";
        }
        return 0;
    }

Url format is myserver/test/post.php, it works perfectly with POST requests, so I believe it doesn't cause the problem.

Data format: name=test&age=18. I am not sure if this works, but this is what I found when I googled.

post.php

if(isset($_POST['name'])){
    $name = $_POST["name"];
    $a = fopen("a.txt", "w");
    fwrite($a, $name);
    fclose($a);
    echo $name;
}else{
    echo "0";
}

Also, this is how I use post function:

string t;
cout << post("myserver/test/post.php", "name=boxxy&age=18", t);
cout << endl << t;

post function returns 1, but nothing happens on PHP side. It always returns "0", which means post request doesn't reach PHP side?

Upvotes: 0

Views: 1169

Answers (1)

stanthomas
stanthomas

Reputation: 1181

This line:

request_stream << "User-Agent: C/1.0";

is missing a newline at the end, i.e.

request_stream << "User-Agent: C/1.0\r\n";

So content-type won't be set correctly and isset($_POST['name']) will be false.

Upvotes: 1

Related Questions