List to form-encoded parameters in Casablanca - c++

I am using cpprestsdk (casabalanca) to POST a request to a server
and I have a list of parameters
std::list<std::pair<string, string>> query;
query.push_back(std::make_pair("val1", "one two"));
query.push_back(std::make_pair("val2", "yo"));
that needs to be encoded as form-encoded parameters.
val1=one%20two&val2=yo
The problem I cannot find a Api to do that (like I have web::json::value for a json payload).
I need to encode each key/value and do the concatenation myself.
There is an Api I am missing out or this simply doesn't exist ?

Found the solution...
web::http::http_request request;
web::uri_builder parameter;
parameter.append_query("val1", "one two", true);
parameter.append_query("val2", "yo", true);
request.set_body(parameter.query(), web::http::details::mime_types::application_x_www_form_urlencoded);

You can use the std::list and fill query parameter
std::string test_url("http://127.0.0.1:9044/api/v1/somepath/");
web::uri_builder ub(test_url);
std::list<std::pair<std::string, std::string>> query;
query.push_back(std::make_pair("val1", "one two"));
query.push_back(std::make_pair("val2", "yo"));
for(auto const &it: query)
{
std::cout<<it.first<<" : "<<it.second<<std::endl;
ub.append_query(it.first, it.second);
}
std::string uri_builder_str = ub.to_string();
http_client client(uri_builder_str);
std::cout<<uri_builder_str<<std::endl;
return client.request(methods::GET);

Related

Put an item in Amazon DynamoDB with AWS SDK CPP using as input the item in JSON format

Is there any easy way to put an item in Amazon DynamoDB with AWS SDK CPP using as input the item in JSON format? Something like
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
request.FunctionThatSetsAllAttributesParsingAJsonString(json_string);
Or is it always necessary to set each attribute and its type?
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
Aws::DynamoDB::Model::AttributeValue val1;
val1.SetS(str);
request.AddItem(key, val1);
Aws::DynamoDB::Model::AttributeValue val2;
...
If the JSON object follows Amazon DynamoDB JSON format, you can easily convert it into an Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> object. For instance, for a Jsoncpp Json::Value object:
Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> json2atts(const Json::Value& json) {
try {
Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> amap;
Aws::Utils::Json::JsonValue jval(Json::FastWriter().write(json));
if (!jval.WasParseSuccessful()) {
throw exception("Failed to parse input JSON");
};
Aws::Utils::Json::JsonView jview = jval.View();
Aws::Map<Aws::String, Aws::Utils::Json::JsonView> jmap = jview.GetAllObjects();
for(auto& i : jmap) {
amap[i.first] = i.second.AsObject();
};
return amap;
}
catch (std::exception &e) {
...
};
};
Then, to put the Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> object:
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
request.SetItem(amap);

c++ JsonCpp parse string with escaped quotes as array

I've got the following json string:
{
"data" :
[
{
"cart" : "[{\"name\":\"Test item 1\",\"price\":15,\"quantity\":1,\"sum\":15,\"tax\":\"none\",\"payment_type\":\"advance\",\"item_type\":\"service\"},{\"name\":\"Test item 2\",\"price\":13.01,\"quantity\":2,\"sum\":26.02,\"tax\":\"none\",\"payment_type\":\"part_prepay\",\"item_type\":\"work\"}]",
"contact" : "noname#google.com",
"p_id" : "603",
"sum" : "100.02",
"tax_system" : "osn"
}
],
"msg" : null,
"result" : "success"
}
I can parse cart as std::string after parsing input json string as stringstream:
const std::string ParseJsonData(std::stringstream ssJsonStream)
{
Json::Value jsonData;
Json::Value responseData;
Json::Value responseDataCart;
Json::CharReaderBuilder jsonReader;
std::string errs;
if (Json::parseFromStream(jsonReader, ssJsonStream, &jsonData, &errs)) {
responseData = jsonData["data"];
responseDataCart = responseData[0]["cart"];
return responseDataCart.toStyledString().c_str();
}
else
return "Could not parse HTTP data as JSON";
}
Please, tell me, how can i parse cart as array with JsonCpp?
The same way you parsed the outer JSON!
You started with a string (well, hidden by a stream) and turned it into JSON.
Now that JSON contains a property that is a string and, itself, contains JSON. The problem is recursive. The fact that the inner string originally came from JSON too can be ignored. Just pretend it's a string you typed in.
So, you can use JSON::Reader to in turn get the JSON out of that string.
Something like:
const std::string responseDataCartStr = responseData[0]["cart"].asString();
Json::Reader reader;
if (!reader.parse(responseDataCartStr, responseDataCart))
throw std::runtime_error("Parsing nested JSON failed");
JsonCpp provides a few ways to parse JSON and it's worth exploring them to find the most appropriate for your use case. The above is just an example.
Ignore the backslashes — the escaping was meaningful within the encapsulating JSON document, but the outermost parse stage should already have taken that into consideration. You'll see if you print responseDataCartStr to console that it is a valid JSON document in its own right.

Not able to get values from JSON in Casablanca, C++

I'm using Casablanca, cpprestsdk to consume REST APIs in C++, in Visual Studio 2015 Professional. I'm trying to develop a simple example here hitting an API and parsing the response as JSON. The URL I'm using, actually returns all the parameters sent to the API.
I've hit the API and got response as well, extracted json from the response successfully. But when i try to read a value at any key from json, it crashes. Hence i put a check whether that key is available or not, and it always says json does not have the field. As an example i printed the data i.e. json. It has the key/field "name" but when i check it via has_field, it returns false.
Please help.
Complete code is below :
json::value postData;
postData[L"name"] = json::value::string(L"Joe Smith");
postData[L"sport"] = json::value::string(L"Baseball");
http_client client(L"https://httpbin.org/post);
http_request request(methods::POST);
request.set_body(postData);
client.request(request).then([](web::http::http_response response) {
json::value j = response.extract_json().get();
json::value data = j.at(U("data"));
std::wcout << "Json : " << data;
// Prints "{\"name\":\"Joe Smith\",\"sport\":\"Baseball\"}"
if (data.has_field(U("name"))) {
std::cout << "Name Found";
}
else {
std::cout << "Name key not Found";
}
});
It seems that your response looks like this:
{ "data": "{\"name\":\"Joe Smith\",\"sport\":\"Baseball\"}" }`
i.e. the actual data is not a JSon object but escaped JSon passed as string. I guess you need to return a payload that looks like this to do what you want to do the way you are doing it:
{
"data": {
"name": "John Smith",
"sport": "Baseball"
}
}

How to get data from Photon eventContent dictionary

We are receiving this callback using ExitGames Photon Realtime engine when an event is fired
customEventAction(int playerNr,
nByte eventCode,
const ExitGames::Common::Object& eventContent)
If the object is a string we use this code to extract it
ExitGames::Common::JString str =
ExitGames::Common::ValueObject<ExitGames::Common::JString>(eventContent).getDataCopy();
However, the object being sent is a dictionary. It's being sent from the server using BroadcastEvent.
How do we get data out of it ?
We've tried this, but it doesn't make any sense:
ExitGames::Common::Dictionary<byte,ExitGames::Common::Object> pdic
= ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<byte,ExitGames::Common::Object>>(eventContent).getDataCopy();
I've found code to get the data from a hashtable, but that doesn't work either.
thanks
Shaun
ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> >(eventContent).getDataCopy();
is absolutely correct and works for me.
The cause of your problem must be inside another line.
When you replace the implementations of sendEvent() and customEventAction() in demo_loadBalancing inside one of the Photon C++ client SDKs with the following snippets, then that demo successfully sends and receives a Dictionary:
send:
void NetworkLogic::sendEvent(void)
{
ExitGames::Common::ValueObject<ExitGames::Common::JString> obj(L"test");
ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic;
dic.put(1, obj);
mLoadBalancingClient.opRaiseEvent(false, dic, 0);
}
receive:
void NetworkLogic::customEventAction(int /*playerNr*/, nByte /*eventCode*/, const ExitGames::Common::Object& eventContent)
{
EGLOG(ExitGames::Common::DebugLevel::ALL, L"");
ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> >(eventContent).getDataCopy();
const ExitGames::Common::Object* pObj = dic.getValue(1);
ExitGames::Common::JString str = ExitGames::Common::ValueObject<ExitGames::Common::JString>(pObj).getDataCopy();
mpOutputListener->write(L"received the following string as Dictionary value: " + str);
}
This gives me the following line of output on the receiving client:
received the following string as Dictionary value: test

making a web services query using gSoap with query arguments

I'm attempting to convert a soap query written for C# into a gSoap query in Visual C++.
The C# query adds an XML node's to the query call, in order to pass parameters to the query:
XmlNode queryOpts = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
queryOpts.InnerXml = "<DateInUtc>TRUE</DateInUtc>";
Here's the C# query, passing various args (some args are specified as XmlNode objects)
XmlNode nodeListItems = listService.GetListItems("Announcements", null, query, viewFields, null, queryOpts, null);
The C++ / gSoap query allows me to pass a query and response object:
listService.__ns10__GetListItems(&announcementQuery, &announcementResponse)
The query object has various properties that can be set that relate to the arguments in the C# call:
announcementQuery.listName
announcementQuery.query
announcementQuery.queryOptions
announcementQuery.viewFields
The first argument there is a string, no problem.
The query, queryOptions and viewFields are a bit confusing.
"query" is a class of type _ns2__GetListItems_query, and it has the following functions & members:
soap_default()
soap_get()
soap_in()
soap_out()
soap_put()
soap_serialize()
soap_type()
__any
__mixed
for query, queryOptions and viewFields, I'd simply like to specify an xml formatted string, like the C# code does, but I'm not sure how this is done.
Can someone cast some experience on this?
thanks!
I'm assuming you've already discovered the answer to this, but I'll post some notes for posterity.
Here's a simple C++ demo for sending and XML doc to a ASP.NET web method.
int _tmain(int argc, _TCHAR* argv[])
{
Service1SoapProxy proxy;
_WebServiceNS1__HelloWorld helloWorld;
_WebServiceNS1__HelloWorld_xml xml;
_WebServiceNS1__HelloWorldResponse response;
xml.__any = "<doc><x>hi</x></doc>";
helloWorld.xml = &xml;
int result = proxy.HelloWorld(&helloWorld, &response);
fprintf(stdout, "result: %i\n", result);
switch(result)
{
case SOAP_OK:
fprintf(stdout, "Response: %s\n", response.HelloWorldResult);
break;
default:
break;
}
return 0;
}
Here's the trivial web method in the .NET service:
[WebMethod]
public string HelloWorld(XmlNode xml)
{
return string.Format("Your XML: {0}", xml.OuterXml);
}
If everything works, you'll see "Response: hi" on your console.