Making a REST request from civetweb

404 views
Skip to first unread message

d.oftheendless

unread,
Nov 1, 2018, 6:25:22 AM11/1/18
to civetweb
Hello All,

Is it possible to make a REST request from civetweb? My use case is to communicate two civetweb application via REST interface and I need to send a REST request from civetweb. 

Civetweb's REST example includes REST request handling, but it does not include any way to send a rest call to another web server. Do you know any way of succeeding this?

I think one option is to establish websocket between two applications but I wonder if I can do it without websocket.

If you have any comment or idea, I would be glad if you can share.

Best regards,

bel

unread,
Nov 1, 2018, 5:33:28 PM11/1/18
to civetweb
You can use the regular client interface, no need to use a websocket.


 
http_https_client(const char *server,
uint16_t port,
int use_ssl,
const char *uri)
{
/* Client var */
struct mg_connection *client;
char client_err_buf[256];
char client_data_buf[LARGE_BUFFER];
const struct mg_response_info *client_ri;
int64_t data_read;
int r;

client = mg_connect_client(
server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
if ((client == NULL) || (0 != strcmp(client_err_buf, ""))) {
error("%s connection to server [%s] port [%u] failed: [%s]",
use_ssl ? "HTTPS" : "HTTP",
server,
port,
client_err_buf); return 0;
}
mg_printf(client, "GET /%s HTTP/1.0\r\n\r\n", uri); // or send JSON request here
r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
if ((r < 0) || (0 != strcmp(client_err_buf, ""))) {
error(
"%s connection to server [%s] port [%u] did not respond: [%s]",
use_ssl ? "HTTPS" : "HTTP",
server,
port,
client_err_buf); return 0;
}
client_ri = mg_get_response_info(client);
if(client_ri == NULL) { error(...); return 0; }
/* Check for status code 200 OK or 30? moved */
if (
client_ri->status_code!=200) { error(...); return 0; }
}
data_read = 0;
while (data_read < client_ri->content_length) {
r = mg_read(client, client_data_buf+data_read , sizeof(client_data_buf)-data_read );
if (r > 0) {
data_read += r;
}
}
/* Nothing left to read */
r = mg_read(client, client_data_buf+data_read , sizeof(client_data_buf)-data_read );
if (r != 0) { error(...); return 0;}
mg_close_connection(client);

JSON_Decode(client_data_buf);

}

bel

unread,
Nov 1, 2018, 5:42:16 PM11/1/18
to civetweb

Well, since quite some time, questions are usually discussed as GitHub issues (https://github.com/civetweb/civetweb/issues) - although many are just questions and not real bugs/issues. A reason might be, on GitHub you can easily post a code snipped, while here it seems quite hard to format it.
You just need to use some JSON library to decode the string.

Sezer BAGLAN

unread,
Nov 20, 2018, 3:04:22 PM11/20/18
to civetweb
Dear all,

First of all thanks for your kind support. The regular client interface works properly in order to communicate civetweb applications. During the communication of two civetweb applications let's say application A has got a GET request from outside world and while handling it -in get handler- we use the regular client interface to transmit that GET request to another civetweb application B. Does this approach make sense? When we try this implementation the communication freezes somehow. Do you have any idea about the possible root cause?

Not that the application A freezes during "mg_printf(bla_bla, "GET /%s HTTP/1.1\r\nContent-Type: application/json\r\n\r\n", uri)" and then time outs. This is working properly when we use regular client interface approach not in civetweb handlers...

Kind Regards,
Sezer

Sezer BAGLAN

unread,
Nov 21, 2018, 2:00:45 AM11/21/18
to civetweb
Dear all,

We've figured out the problem. The problem is not related with regular client interface; it's still working properly. It's related with our own design. Sorry for false alarm.

Kind regards,
Sezer
Reply all
Reply to author
Forward
0 new messages