Incorrect POST format

Oct 9, 2015 at 5:30 AM
I'm trying to create a post to a server to send an SMS message and what comes out of CPPREST is not correctly formatted. The code below makes the request to my company's Web server, which has no idea what do do with it, but allows me to capture the request in WireShark.
#include <cpprest/http_client.h>

int wmain(int argc, wchar_t *argv[])
{
    using namespace ::std;
    using namespace ::web::http;
    using ::web::http::client::http_client;

    uri_builder base_uri(U("http://edulinksys.com"));
    base_uri.set_user_info(U("username:password"));

    http_client client(base_uri.to_uri());

    uri_builder request_uri(U("/sms"));
    request_uri.append_query(U("destinationAddress"), "2125551212")
        .append_query(U("sourceAddress"), U("2125551213"))
        .append_query(U("message"), U("test message"));

    http_request request(methods::POST);
    auto &headers = request.headers();
    headers.add(header_names::content_type, U("application/x-www-form-urlencoded"));
    headers.add(header_names::accept, U("application/json"));
    request.set_request_uri(request_uri.to_uri());

    client.request(request).then(
    [](http_response response)
    {
        wcout << response.to_string();
    }).wait();

    return 0;
}
The request looks like this on the wire:
POST /sms?destinationAddress=2125551212&sourceAddress=2125551213&message=test%20message HTTP/1.1
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Accept: application/json
User-Agent: cpprestsdk/2.6.0
Content-Length: 0
Host: edulinksys.com
My admittedly less than expert level of understanding tells me that this is not the correct format. The first line should be approximately "POST /sms HTTP/1.1" and the query string should be in the body, under the "Host:..." line.

I have tried numerous ways to put the request together with uri_builders, http_clients and http_requests; all produce the same results. Who's wrong, me or CPPREST?

-evan
Oct 9, 2015 at 6:41 PM
Surprise, surprise, it's me that's wrong. Apparently, if you want some text in the body of an HTTP request you need to actually put it there. And if you don't want some text in the URI you shouldn't pass it to http_request::set_request_uri().

So far, changing the line "request.set_request_uri..." to request.set_body(request_uri.query())" seems to be the best way to get the formatted query text.

-evan
Marked as answer by kavyako on 10/9/2015 at 11:33 AM
Oct 9, 2015 at 7:33 PM
Correct, this behavior is different for HTTP GET and HTTP POST requests.
The HTTP GET request method is designed to retrieve information from the server. As part of a GET request, some data can be passed within the URL's query string. As part of a POST request, an arbitrary amount of data of any type can be sent to the server in the body of the request message.

Glad you got it working!

Thanks
Kavya