POST method syntax

Jun 14, 2013 at 1:24 PM
Hello,
I need to send a text to the server using the POST method. I used the following code -

pplx::task<http_response>resp = client.request(methods::POST,L"", L"This is the text that I want to send",L"text/plain");
http_response response = resp.get();
int stcode = response.status_code();

(It is synchronous calling)
It is getting compiled but no request is sent to the server. However, if I just send -

pplx::task<http_response>resp = client.request(methods::POST);

instead of the earlier "resp", then the request is sent to the server.

Can anyone guide me the accurate syntax of sending a plain text to the server using POST method
Coordinator
Jun 14, 2013 at 5:08 PM
Hi viabhav3369,

I tried out your code snippet in both desktop and Windows store applications with Visual Studio 2012 and it ran fine. The code itself looks correct as well. More information about your environment would help. What version of Casablanca, what configuration and environment does this happen in? A small FYI you don't need to specify the content type in this case, when using a string it defaults to 'text/plain'. You could just use:
pplx::task<http_response>resp = client.request(methods::POST, L"", L"This is the text that I want to send");
Can you describe the behavior you are seeing when you execute that code. Are you getting back a status code or is the request timing out?

Thanks,
Steve
Jun 14, 2013 at 5:31 PM
Hi Steve,
Thanks for the response.
I made a change in the URL of the client and now I'm able to connect to the server.
But now, in the server when I read the body of my request, then I get [object Object].
I'm using Casablanca for VS 2012 and the server is running using node.js

Thanks,
Vaibhav
Coordinator
Jun 17, 2013 at 9:21 PM
Hi Vaibhav,

I don't understand what you mean by when you read the body of the request you get [object Object]. I put together a quick node.js server and didn't have any difficult using our http_client with it. Can you provide some more details or what the node.js code looks like?

Here is the http client code I'm using:
#include <http_client.h>

using namespace utility;
using namespace web::http;
using namespace web::http::client;

int wmain(int argc, wchar_t *args[])
{
    http_client client(L"http://localhost:45678");

    pplx::task<http_response> resp = client.request(methods::POST, L"", L"This is the text that I want to send"); 
    http_response response = resp.get(); 
    int stcode = response.status_code(); 
    stcode;

    return 0;
}
Here is the node.js server:
var http = require('http');
http.createServer(function (req, res) {
    console.log("[200] " + req.method + " to " + req.url);
    var body = '';
    
    req.on('data', function(chunk) {
        body += chunk.toString();
    });
    
    req.on('end', function() {
        console.log("Received: " + body);
    
      res.writeHead(200, "OK");
      res.end();
    });
}).listen(45678, '127.0.0.1');
console.log('Server running at http://127.0.0.1:45678/');
When running I get the following output showing the request body being read and printed to the console:
Server running at http://127.0.0.1:45678/
[200] POST to /
Received: This is the text that I want to send

Thanks,
Steve
Dec 4, 2014 at 6:33 PM
Hello Steve,

Saw you video on using C++ to connect to a web service on you-tube. Very helpful. Thanks..
My question is, from the code or set up on the you-tube video, how can i send a file or its content as the request to a sever rath then just strings i type out?
my issue is the syntax. Code below


int _tmain(int argc, _TCHAR* argv[])

{
//gets file data and converts it to string
string data = get_file_contents( "my file with content");

//connection to server
http_client client (U("my restful webservice"));

//sending data as request, will like to send file and its content rather than just this, converting data to string and sending it is fine too
http_response response = client.request(methods::POST, U(data)).get();...this is where i get the error with my syntax

//response from web service
if(response.status_code() == status_codes::OK)
{
    const utility::string_t body = response.extract_string().get();
    std::wcout<<body;
}


getchar();

return 0;
}

Also can i send the file content as string or have to convert it to string_t?

Thank you for a speedy response, this is the last part of my work.

Regards,

Steven
Coordinator
Dec 5, 2014 at 5:14 PM
Hi Steven,

I'm not sure which version you are using but in 2.3.0 we added some overloads on Windows to work with UTF-8 strings with the http_client using the correct http_client::request overload. It looks like you are missing the path portion and should be able to directly pass the std::string. Try something like the following:
http_response response = client.request(methods::POST, "path", data).get();
If you want to upload from a file, instead of reading first into a string it probably would be more efficient to directly open a stream to the file and use that instead. Here is the correct http_client::request overload. Try something like the following:
auto stream = concurrency::streams::file_buffer<uint8_t>::open(U("filename"), std::ios_base::in).get();
http_response response = client.request(methods::POST, U"path", stream, U("content type")).get();
Also in the future please open up a new discussion thread as opposed to recycling an older one.

Thanks,
Steve
Dec 5, 2014 at 7:39 PM
Edited Dec 6, 2014 at 4:26 PM
Hi Steve,

Will open new discussion in the future if needed. My syntax was wrong but your update helped me get it right. Thank you so so so so much. Have a great weekend.

Regards,

Steven