| 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); | |
| } |