Send a post request with libcurl C++ question :s - c++

I've succeeded in my GET request with libcurl, no problems!
However when I try to send a post request, I'm unsure of where I put the json data, I want to be sent over..
My code looks like this, and I wonder if there is a method where I can send over the data as is:
CURL* curlpost;
curl_global_init(CURL_GLOBAL_ALL);
curlpost = curl_easy_init();
if (curlpost) {
curl_easy_setopt(curlpost, CURLOPT_URL, "https://127.0.0.1:50006/lol-lobby/v2/lobby");
// post data:
curl_easy_setopt(curlpost, CURLOPT_POSTFIELDS, "{
"customGameLobby": {
"configuration": {
"gameMode": "CLASSIC", "gameMutator" : "", "gameServerRegion" : "",
"mapId" : 11,
"mutators" : {"id": 1}, "spectatorPolicy" : "AllAllowed", "teamSize" : 5
}
},
"queueId": 830,
"isCustom" : false
}")
That doesn't work and is the raw JSON data I want to send to the server.
How can I send that data to the server is my question?
JSON data to be sent:
{
"customGameLobby": {
"configuration": {
"gameMode": "CLASSIC",
"gameMutator": "",
"gameServerRegion": "",
"mapId": 11,
"mutators": {"id": 1},
"spectatorPolicy": "AllAllowed",
"teamSize": 5
}
},
"queueId": 830,
"isCustom": false
}

Use curl_easy_escape to URL encode the given C string:
char* encoded = curl_easy_escape(curlpost, "{ your post data }", 0);
// use "encoded"
// free the memory
curl_free(encoded);

Related

Uploading file using cpp-httplib

I am using cpp-httplib and I am trying to upload a file to a post test server like https://ptsv2.com . I do not really understand the given documentation on file uploads, I somewhat understand that you have to make use of Multipart/form-data in order to upload a file.
The given code in the documentation is this
httplib::MultipartFormDataItems items = {
{ "text1", "text default", "", "" },
{ "text2", "aωb", "", "" },
{ "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
{ "file2", "{\n \"world\", true\n}\n", "world.json", "application/json" },
{ "file3", "", "", "application/octet-stream" },
};
auto res = cli.Post("/multipart", items);
in file1 you can see that it is creating a file on the server and naming it hello.txt.
How do I write it so that I can upload a file that is located on my device onto a server?
Any advice would be helpful. Thank you.
according to #Botje and cpp-httplib documentation
std::ifstream t_lf_img("lfimg.png");
std::stringstream buffer_lf_img;
buffer_lf_img << t_lf_img.rdbuf();
std::ifstream t_pc_file("pc.ply");
std::stringstream buffer_pc_file;
buffer_pc_file << t_pc_file.rdbuf();
httplib::Client cliSendFiles("http://ip::port");
httplib::MultipartFormDataItems items = {
{"files", buffer_lf_img.str(), "lf.png", "application/octet-stream"},
{"files", buffer_pc_file.str(), "truck.ply", "application/octet-stream"},
};
auto resSendFiles = cliSendFiles.Post("/uploadfile", items);

Postman: How to validate a response to have a correct date format

Just can´t find how to validate the response to be in a date format like: 2014-07-14T21:15:58+00:00
pm.test("Validate dateCreated", function () {
pm.expect(jsonData.data[0].dateCreated == ("YYYY-MM-DDTHH:mm:ss")); // timestamp
});
This is my awfull aproach, it validates the result even if I change operators or the date format in the assertion,I tried with $timestamp, and other examples here, but none seems to work please help, thx
The response body is like this
{
"current_page": 1,
"data": [
{
"rn": "1",
"id": 2,
"mxrId": null,
"dateCreated": "2014-07-14T21:15:58+00:00",
"dateModified": "2018-03-16T21:15:13+00:00",
"svsId": "1",
"token": "08A96E05",
"name": "Original Test",
"clientId": 1675,
"projectId": 1,
And this is what i came with, if you find a better way, please let me know.
let dateFormat = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
pm.test("Validate dateCreated", function () {
pm.expect(jsonData.data[0].dateCreated).to.match(dateFormat);
});

Setting output contexts in Dialogflow

Using the C# client library for Dialogflow, I am trying to set the output context in a webhook response. However, the output context field is read only. This is my code:
WebhookResponse response = new WebhookResponse
{
FulfillmentText = "This is a test",
OutputContexts = ... //Regardless of what I try and set OutputContexts to be, I get the error "property or indexer 'WebhookResponse.OutputContexts' cannot be assigned to -- it is read only"
};
How do I set the output context?
I know this is an old question but just in case someone has the same problem.
You can not assign a new list to OutputContexts, you have to add them to the list:
For example:
response.OutputContexts.Add(new Context
{
Name = $"{request.Session}/your_context",
LifespanCount = 1
});
I think the response json which you are forming is wrong.
Below is the correct json response which you need to send:
{
"fulfillmentText = "This is a test",
"outputContexts": [
{
"name": "projects/project_id/agent/sessions/session_id/contexts/your_context",
"lifespanCount": 5,
"parameters": {
"foo": "bar",
"foo1": "bar1"
}
}
],
"followupEventInput": {
"name": "even_name"
}
}

How to decrypt an encrypted payload in hyperledger?

I am currently using Hyperledger Fabric. I am using the REST API to make GET request as so:
curl 172.18.0.3:7050/chain/blocks/31
And the output I am getting back is :
{
"transactions": [
{
"type": 2,
"chaincodeID": "BMBQHHg2y0RnadYEaZZT8icjMvZbDPjkn5mFb+clFORxJqz8qsMs/QlalCT+A3msuc59KYM5sbZyhM3OeSplTWo91WAHTUgqIKVrm1gUzsouBIqLNvpqgimN36+s0ywF0Rx4gn27RmQYBbB+877Nh+w7A8Ezz92T1MgHcmzfRgVaDmiN0ga+jAfufNYglmeM4ZSysmSsz6xJtrcD5mTmHXZtvtw6uGCI1TCOMBaWTpLhNHfM2/5EB5jatdMjDi1GAlaXkDWcLgGjScL1yZpWcntz/N0cT90r6i9ycXZ0kk9wodBq2cFutDTdkl8S90kzd0gXig==",
"payload":"BBYZD6S/hRILcf21zVbhMAhA+qLQvAq+KvOBuXOknPCAMjas2LI9f42AKG6r+uWP71LYEkbo1XXANuDmukZjDsFGltzoIfq+Mry5n/CNXzXgiVLX0J7z08kGfEfw2vnywgmVFX4UtKPpl8pMTmRxJWn5Q0HY1pFnA6ZaXluoLRf7f17Ko4SPahi19k2NszcJ0SHE7xRllfLXZJxaOlT2J56nqjTBKTJ86bdqn6AdQXHA6Px7yz5XpgJhccyecaLS4sYcsrqHoOlO+kk+bw5Q6qnkHfIIhLXCEgHxKoT00L8I8B2luO1RlmQd4mNfXb7GrLOJXvCNPrcpSEmQDByEGwn1j3Zy0lilwKVaNYTPNThMwQ==",
"txid": "72bd2ab7-f769-49c9-a754-c7be0c481cf0",
"timestamp": {
"seconds": 1496062124,
"nanos": 474977395
},
"confidentialityLevel": 1,
"confidentialityProtocolVersion": "1.2",
"nonce": "2YgU+0WYPuTKGsKkT1hx7McOURPTIRgG",
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Om3zyXvGb4YCCt7I7+M0gZF4xzAiAgYuCf7FPGx3fnJdABlZjszA1pR6jaPtIOQN2ndfAFZA==",
"signature": "MEUCIQCVBtfjk3yzwfOFyOojH5tynq3HrG7dFN9URXB5C6kYDAIgLPcwJBAIVlD1I4dxzczfxmywlZn1ZMSvL2djioWgqFQ="
}
],
"stateHash": "9KEsiBp4t/VZyETXMASSYtuPuf8JowktCSbX7daPt69uqDzrJvifrPIXpI5N1kOayoq6H0afM8zN/WZpWsesHQ==",
"previousBlockHash": "v6Fo6SARD0xdE0B/jvIq22kgV5uLAKhTwLjrA4YRBskWcZOjECFbNgzlwFQhEmbar1zcAbcZVo9eo/3tx2y68g==",
"consensusMetadata": "CCA=",
"nonHashData": {
"localLedgerCommitTimestamp": {
"seconds": 1496062125,
"nanos": 496018341
},
"chaincodeEvents": [
{
},
{
}
]
}
}
So I had performed a invoke to
transfer 10 from a to b.
And i got this payload.
The payload is encrypted as the
CORE_SECURITY_ENABLED=true and
CORE_SECURITY_PRIVACY=true
I know we have to use the certificate to decrypt the payload and then might be use base64 decoding to get the exact payload back.
But my question is what are the exact function calls or exact steps involved in doing so ?

change json structure in Django

I've never had to change json structure before using Django models, so I may have made a completely stupid question.
I have some data in my database and want to parse it in a json format.
My view.py (a bit simplified) is:
def get_json(request):
pos = Pos.objects.filter(id=1).values('lat', 'lon','id')
return JsonResponse ({'position': list(pos)})
As a result I get this json object:
{ "position": [{"lat": "21", "id": 1, "lon": "21"}, {"lat": "22", "id": 1, "lon": "22"}, {"lat": "23", "id": 1, "lon": "23"}]}
In order to reduce the volume of unusefull data, I need to get a json structure like this:
{"id":"1", "position":{"lats":[21,22,23], "longs":[21,22,22]} }
I would be gratefull if sombody could help me
You'll need to process the data; you can do whatever you like within the view function.
pos = Pos.objects.filter(id=1).values('lat', 'lon','id')
data = {"lats": [], "longs": []}
for p in pos:
data["lats"].append(p['lat'])
data["longs"].append(p['long'])
return JsonResponse({'position': data})