Le 13/04/2012 22:28, Tommi Mäkitalo a écrit :
> Hi Jean-Michel,
>
> the main problem is, that you misinterpreted this
> "request.sendBody(std::ostream&)". The method sends the currently set
> body to the passed std::ostream. And until your call the body is empty
> and so nothing in appended to your data. The right way to set the body
> is to use "request.body()", which returns a std::ostream, which receives
> the body. So this is the fixed code:
>
> #include<iostream>
> #include<string>
> #include<cxxtools/http/request.h>
> #include<cxxtools/http/reply.h>
> #include<cxxtools/http/client.h>
>
> int main(int argc, char *argv[])
> {
> try
> {
> cxxtools::http::Request request("/rest");
> request.addHeader("Content-Type", "application/json");
> request.method("PUT");
> request.body()<< "{ \"s\": { \"_str\": \"STRUCT\", \"_vec\": [ 1,
> 2, 3, 4 ], \"_int\": 10 } }";
>
> cxxtools::http::Client client("", 8000);
> client.execute(request);
> std::cout<< client.readBody();
> }
> catch (const std::exception& e)
> {
> std::cerr<< e.what()<< std::endl;
> }
> }
>
> I made some other changes as well:
>
> I always put this try-catch block into my main function. Otherwise when
> something happens like the server is not running the client crashes.
> Using this try-catch block I get a nice error message to std::cerr.
>
> I never use "using namespace". I personally do not like it. Namespaces
> are used for separating namespaces. "using namespace" somewhat
> invalidates that feature. Especially "using namespace std" is not
> allowed in headers, so why should I use it in cpp-files then? If I
> would, I would use a different style in headers and cpps. But it is just
> a matter of taste. You can decide to use it if you prefer.
>
> A "return 0" in main is not needed. As a special exception to other
> functions, the main function returns 0 by default if nothing else is
> explicitly returned. But some people prefer the explicit "return 0".
> Again just a matter of taste.
>
> On the client side a empty string means any localhost. The string
> "localhost" normally means just IPv4 localhost. If you use the empty
> string, it may connect with IPv6 if the system is configured for that.
> On the server side a empty IP adress means all local interfaces. Also
> IPv6. Therefore tntnet configured the listen interface with a empty IP
> by default.
>
> I see, that you want to use json. Cxxtools has a nice json serializer
> and deserializer for that. Here is the same example as above but using
> the json serializer:
>
> #include<iostream>
> #include<string>
> #include<vector>
> #include<sstream>
> #include<cxxtools/serializationinfo.h>
> #include<cxxtools/jsonserializer.h>
> #include<cxxtools/http/request.h>
> #include<cxxtools/http/reply.h>
> #include<cxxtools/http/client.h>
>
> struct S
> {
> std::string str;
> std::vector<int> vec;
> int number;
> };
>
> void operator<<= (cxxtools::SerializationInfo& si, const S& s)
> {
> si.addMember("_str")<<= s.str;
> si.addMember("_vec")<<= s.vec;
> si.addMember("_int")<<= s.number;
> }
>
> int main(int argc, char* argv[])
> {
> try
> {
> std::ostringstream data;
> cxxtools::JsonSerializer serializer(data);
> S s;
> s.str = "STRUCT";
> s.vec.push_back(1);
> s.vec.push_back(2);
> s.vec.push_back(3);
> s.vec.push_back(4);
> s.number = 10;
> serializer.serialize(s, "s");
> serializer.finish();
>
> cxxtools::http::Request request("/rest");
> request.addHeader("Content-Type", "application/json");
> request.method("PUT");
> request.body()<< data.str();
>
> cxxtools::http::Client client("", 8000);
> client.execute(request);
> std::cout<< client.readBody();
> }
> catch (const std::exception& e)
> {
> std::cerr<< e.what()<< std::endl;
> }
> }
>
> One of the big advantages is automatic encoding of data. For the json
> serializer it is no problem, if the string contains a quote itself. It
> is automatically escaped accordingly. If you concatenate just your data
> together, you may break json, if the data contains certain characters.
>
> Tommi
>
> ------------------------------------------------------------------------------
> For Developers, A Lot Can Happen In A Second.
> Boundary is the first to Know...and Tell You.
> Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
> http://p.sf.net/sfu/Boundary-d2dvs2
> _______________________________________________
> Tntnet-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/tntnet-general
Hi Tommi,
Thank you for these tips!
Tommi, put your examples on the wiki, I think it may be useful...
Jean-Michel
------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general