post and get json in c restful webservice - c

I read tutorial http://www.vogella.com/tutorials/REST/article.html , i wonder how to post and get json from client with C or C++ without use jerson-client library.Thanks a lot.

For C/C++ Development, You can use libcurl library for making http request. refer [here]. http://curl.haxx.se/libcurl/
For JSON, jsoncpp can be used.see here https://github.com/open-source-parsers/jsoncpp

I have 2 link for C ++ Restful
step 1: add c++ rest sdk using NUGET (name: casablanca)
https://casablanca.codeplex.com/wikipage?title=Using%20NuGet%20to%20add%20the%20C%2b%2b%20REST%20SDK%20to%20a%20VS%20project
step 2: Http Client Tutorial
http://www.codeproject.com/Articles/603810/Using-Casablanca-to-consume-a-REST-API
before test this code you need to change host (http:......) that belongs to you
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <conio.h>
#include <stdio.h>
#include <string>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
//==================GET and save in file abc.txt
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("abc.txt")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://localhost:8080/JSonJersey/rest"));
// Build request URI and start the request.
uri_builder builder(U("/getEmployee"));
// builder.append_query(U("q"), U("Casablanca CodePlex"));c
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// ================POST
pplx::task<int> Post()
{
return pplx::create_task([]
{
json::value postData;
postData[L"id"] = json::value::number(13);
postData[L"firstName"] = json::value::string(L"Baseball");
postData[L"lastName"] = json::value::string(L"hello");
postData[L"age"] = json::value::number(32);
http_client client(L"http://localhost:8080/JSonJersey/rest/class");
return client.request(methods::POST, L"/PostJsonEmployee", postData.to_string().c_str(), L"application/json");
}).then([](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
return 0;
});
}
int main(int argc, char* argv[])
{
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
Post().wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
getch();
return 0;
}

Related

TensorFlow C API Logging Setting

I am trying to suppress the logging of the tensorflow in C-API when it loads a saved model. The logging looks like this
2020-07-24 13:06:39.805191: I tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: /home/philgun/tf-C-API/my_model
2020-07-24 13:06:39.806627: I tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
2020-07-24 13:06:39.819994: I tensorflow/cc/saved_model/loader.cc:202] Restoring SavedModel bundle.
2020-07-24 13:06:39.875249: I tensorflow/cc/saved_model/loader.cc:151] Running initialization op on SavedModel bundle at path: /home/philgun/tf-C-API/my_model
2020-07-24 13:06:39.884401: I tensorflow/cc/saved_model/loader.cc:311] SavedModel load for tags { serve }; Status: success. Took 79210 microseconds.
Below is the part of my code that loads the saved model
//*********************Read Model
TF_Graph* Graph = TF_NewGraph();
TF_Status* Status = TF_NewStatus();
TF_SessionOptions* SessionOpts = TF_NewSessionOptions();
TF_Buffer* RunOpts = NULL;
const char* tags = "serve"; // default model serving tag
int ntags = 1;
TF_Session* Session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, Graph, NULL, Status);
Since there's so little documentation on TF C-API, I am now stuck in this problem. Does anybody know how to do it?
After some hustling I found a way to do it by setting a new environment variable called TF_CPP_MIN_LOG_LEVEL. Here's how I did it:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tensorflow/c/c_api.h"
int main()
{
<your main code>
}
void CallSavedModel(double raw_input[], int inputsize, char* saved_model_dir)
{
char* new_environment = "TF_CPP_MIN_LOG_LEVEL=3";
int ret;
ret = putenv(var);
IMPORT YOUR SAVED MODEL START FROM HERE
}
I got the answer by combining https://pubs.opengroup.org/onlinepubs/009695399/functions/putenv.html and Disable Tensorflow debugging information
Cheers!
Hope this is helpful for those who faced the same headache like I had.
Phil

IPC : Node JS server communication with simple C code

I have a C code, that within a infinite loop waits for an input and produces an output.
#include<stdio.h>
void flush() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
/*
* Load many files here
*/
double d = 0;
char input[1024];
while (1) {
d = d + 0.1;
scanf("%[^\n]", input);
printf("%lf\n",d);
fflush(stdout);
flush();
}
}
I need another Node JS service that will listen to on some port and send the output as response.
I have this code written
var http = require('http');
var spawn = require('child_process').spawn;
var child = spawn('./dummyNLU.out');
child.stdin.setEncoding('utf-8');
child.stdin.cork();
var buffer = new Buffer(100);
var app = http.createServer(function(req,res){
var string = buffer.write("HelloWorld!"); //this is for testing purpose
child.stdin.write(buffer);
child.stdin.end();
child.stdout.on('data', function(data) {
res.setHeader('Content-Type', 'text/plain');
res.end(data);
});
});
app.listen(3001);
This code I have doesn't seem seem working at all.
Node JS server terminates with an error and the web response consist of 283 lines of response instead of 1.
Can anyone please help me?? Some other approaches to solve the problem (reply to web request from a C executable code output) are also welcome.
Thank you in advance.
child_process.spawn
Spawn your C code binary as a child from Node (bidirectional communication)
var spawn = require('child_process').spawn
var child = spawn('main.exe')
child.stdin.end('7')
child.stdout.on('data', (data) => { console.log(data); })
child.on('close', (code) => console.log('Exit code: ' + code))
This can be tricky if its receiving multiple simultaneos requests, You will need to track the client (origin request) to do the correct response (answer to the correct requesting client)
Or make a queu and spawn a children for each request (having a maximum of X simultaneos children working at a time) so they are spawned as requested and killed once not needed (having the queu is to throttle the total number of active C process)
PoC node spawning a child command (ls) and printing the result (stdout of the spawned process)
const spawn = require('child_process').spawn
const C = spawn('ls');let r=''
C.stdout.on('data',d=>r+=d)
C.on('close', () => console.log(r));
streaming-worker
designed to give you a simple interface for sending and receiving
events/messages from a long running asynchronous C++ Node.js Addon.
Streaming data from C to Node.js

How can I write, read and append parameter-value pairs of HTTP request to a file at Apache's server side?

I am a newbie, and I'm writing an Apache module to handle http request. I want to store all of the http requests' parameters and their values to a txt file. And, here is my code, but it didn't succeeded.
Whenever, I tried to put some parameters in the URL, the page crashed. In the error.log of apache, it shows "Segmentation Fault (core dumped)". I am aware that there is API of APR_FILE_() here instead of using standard C library. But I am so dump to understand all of pointers of those functions and the API does not have any sample usage of the apr_file_() functions, thus being stuck with file I/O. So I would be really thankful if someone could help me with the problem.
For your ready reference: I paste my code here to:
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* The sample content handler */
static int learning_handler(request_rec *r)
{
/* First off, we need to check if this is a call for the "example-handler" handler.
* If it is, we accept it and do our things, if not, we simply return DECLINED,
* and the server will try somewhere else.
*/
//if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
/* Set the appropriate content type */
ap_set_content_type(r, "text/html");
/* Print out the IP address of the client connecting to us: */
ap_rprintf(r, "<h2>Hello, %s!</h2>", r->useragent_ip);
/* If we were reached through a GET or a POST request, be happy, else sad. */
if ( !strcmp(r->method, "POST") || !strcmp(r->method, "GET") ) {
ap_rputs("You used a GET or a POST method, that makes us happy!<br/>", r);
}
else {
ap_rputs("You did not use POST or GET, that makes us sad :(<br/>", r);
}
/* Lastly, if there was a query string, let's save it file */
if (r->args) {
ap_rprintf(r, "Your query string was: %s", r->args);
FILE * fp;
fp = fopen ("params_file.txt", "w+");
fprintf(fp, "%s",r->args);
fclose(fp);
}
return DECLINED;
}
static void learning_register_hooks(apr_pool_t *p)
{
ap_hook_handler(learning_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA learning_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
learning_register_hooks /* register hooks */
};

Parse post data with punctuation incorrectly in golang

I know how to parse post data in golang
r.ParseForm()
pid := r.PostFormValue("pid")
code := r.PostFormValue("code")
lang := r.PostFormValue("lang")
author := r.PostFormValue("author")
But the post data is pid=1&code=#include <stdio.h>\x0Aint main()\x0A{\x0A\x09printf(\x223\x5Cn\x22);\x0A\x09return 0;\x0A}&lang=c&author=11(this is obtained from nginx log)
So when I parse the data, it could be wrong. The parsed data of code is
#include <stdio.h>
int main()
{
printf("3\n")
instead of
#include <stdio.h>
int main()
{
printf("3\n");
return 0;
}
So how can I fix this problem?
You're passing the raw code, which could be unsafe, your problem is because of this:
https://golang.org/src/net/url/url.go?s=21047:21092#L761
I would suggest that you base64encode your log code and then decode it in your handler.
import "encoding/base64"
...
code, err := base64.RawURLEncoding.DecodeString(r.PostFormValue("code"))
if err != nil {
// handle error
}
Then your request should look like this:
curl --data "pid=1&code=I2luY2x1ZGUgPHN0ZGlvLmg-XHgwQWludCBtYWluKClceDBBe1x4MEFceDA5cHJpbnRmKFx4MjIzXHg1Q25ceDIyKTtceDBBXHgwOXJldHVybiAwO1x4MEF9&lang=c&author=11" http://localhost:8080

connection gwan with aerospike db in C

Hello.
First I'm sorry for my ita-english.
I want use gwan with aerospike but when run the servlet...problem.
I start with this example.c of aerospike. In file example.c I put gwan.h and this is the output ./gwan:
loading
hello.cs: to use .cs scripts, install C#..
hello.lua: to use .lua scripts, install Lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linking example.c: undefined symbol: g_namespace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To run G-WAN, you must fix the error(s) or remove this Servlet.
Inside example.c:
#include "gwan.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <aerospike/aerospike.h>
#include <aerospike/aerospike_key.h>
#include <aerospike/aerospike_query.h>
#include <aerospike/as_error.h>
#include <aerospike/as_key.h>
#include <aerospike/as_query.h>
#include <aerospike/as_record.h>
#include <aerospike/as_status.h>
#include <aerospike/as_val.h>
#include "example_utils.h"
const char TEST_INDEX_NAME[] = "test-bin-index";
bool query_cb(const as_val* p_val, void* udata);
void cleanup(aerospike* p_as);
bool insert_records(aerospike* p_as);
int
main(int argc, char* argv[])
{
if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {
exit(-1);
}
aerospike as;
example_connect_to_aerospike(&as);
example_remove_test_records(&as);
example_remove_index(&as, TEST_INDEX_NAME);
if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME))
{
cleanup(&as);
exit(-1);
}
if (! insert_records(&as)) {
cleanup(&as);
exit(-1);
}
if (! example_read_test_records(&as)) {
cleanup(&as);
exit(-1);
}
as_error err;
as_query query;
as_query_init(&query, g_namespace, g_set);
as_query_where_inita(&query, 1);
as_query_where(&query, "test-bin", as_integer_equals(7));
LOG("executing query: where test-bin = 7");
if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL)
!= AEROSPIKE_OK) {
LOG("aerospike_query_foreach() returned %d - %s", err.code,
err.message);
as_query_destroy(&query);
cleanup(&as);
exit(-1);
}
LOG("query executed");
as_query_destroy(&query);
cleanup(&as);
LOG("simple query example successfully completed");
return 0;
}
bool
query_cb(const as_val* p_val, void* udata)
{
if (! p_val) {
LOG("query callback returned null - query is complete");
return true;
}
as_record* p_rec = as_record_fromval(p_val);
if (! p_rec) {
LOG("query callback returned non-as_record object");
return true;
}
LOG("query callback returned record:");
example_dump_record(p_rec);
return true;
}
void
cleanup(aerospike* p_as)
{
example_remove_test_records(p_as);
example_remove_index(p_as, TEST_INDEX_NAME);
example_cleanup(p_as);
}
bool
insert_records(aerospike* p_as)
{
set
as_record rec;
as_record_inita(&rec, 1);
for (uint32_t i = 0; i < g_n_keys; i++) {
as_error err;
as_key key;
as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);
as_record_set_int64(&rec, "test-bin", (int64_t)i);
if (aerospike_key_put(p_as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {
LOG("aerospike_key_put() returned %d - %s", err.code, err.message);
return false;
}
}
LOG("insert succeeded");
return true;
}
how can connect aerospike with gwan?
Thank you
You need to #pragma link your aerospike library, and make sure all your required header files are in the right place. See G-WAN FAQ or read example code in the G-WAN tarball.
Also, in G-WAN the return code of the main function will be used as HTTP response code, so avoid return -1;.
undefined symbol: g_namespace
the error message is clear. As long as this variable is undefined your C servlet won't compile.
I don't know your library but this variable is probably defined in a library include file - or must be defined by the end user (you). Check the library documentation.
Detailed steps to run Aerospike C-client example with G-WAN,
Download and extract G-WAN server tar on your system
You can start the G-WAN server using ./gwan script present in extracted folder, e.g. ./gwan_linux64-bit/
Get Aerospike C-client from https://github.com/aerospike/aerospike-client-c, and install on your system
Copy example.c to ./gwan_linux64-bit/0.0.0.0_8080/#0.0.0.0/csp/
Make following changes to example.c,
Add following #pragma directive,
#pragma include "/home/user/aerospike-client-c/examples/utils/src/include/"
This will help search example_utils.h, which is necessary for all the example scripts in C-client.
Add following #pragma directive,
#pragma link "/home/user/aerospike-client-c/examples/utils/src/main/example_utils.c"
We shall have to link example_utils.c, as it has definitions of all util functions used in example scripts.
Make changes to the return values. Retun proper HTTP error codes.
Now, you are good to go. Run ./gwan server and access your webservice through browser, http://127.0.0.1:8080/?example.c

Resources