Ciro Edgardo Romero
/
ISE_CEIoT
Proyecto integrador para asignatura de Especialización
main.cpp@36:00496782708e, 2021-04-11 (annotated)
- Committer:
- ciror00
- Date:
- Sun Apr 11 02:17:20 2021 +0000
- Revision:
- 36:00496782708e
Trabajo integrador de Especializacion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ciror00 | 36:00496782708e | 1 | //=====[Libraries]============================================================= |
ciror00 | 36:00496782708e | 2 | |
ciror00 | 36:00496782708e | 3 | // Se incluye como parte de la la clase HTTP |
ciror00 | 36:00496782708e | 4 | #include "select-demo.h" |
ciror00 | 36:00496782708e | 5 | #if DEMO == MAIN |
ciror00 | 36:00496782708e | 6 | |
ciror00 | 36:00496782708e | 7 | #include "mbed.h" |
ciror00 | 36:00496782708e | 8 | #include "http_request.h" |
ciror00 | 36:00496782708e | 9 | #include "network-helper.h" |
ciror00 | 36:00496782708e | 10 | #include "mbed_mem_trace.h" |
ciror00 | 36:00496782708e | 11 | |
ciror00 | 36:00496782708e | 12 | #include <stdio.h> |
ciror00 | 36:00496782708e | 13 | #include <string.h> |
ciror00 | 36:00496782708e | 14 | |
ciror00 | 36:00496782708e | 15 | //=====[Declaration and initialization of public global objects]=============== |
ciror00 | 36:00496782708e | 16 | |
ciror00 | 36:00496782708e | 17 | //=====[Declaration and initialization of public global variables]============= |
ciror00 | 36:00496782708e | 18 | |
ciror00 | 36:00496782708e | 19 | //=====[Declarations (prototypes) of public functions]========================= |
ciror00 | 36:00496782708e | 20 | |
ciror00 | 36:00496782708e | 21 | void response(HttpResponse* res); |
ciror00 | 36:00496782708e | 22 | |
ciror00 | 36:00496782708e | 23 | //=====[Main function, the program entry point after power on or reset]======== |
ciror00 | 36:00496782708e | 24 | |
ciror00 | 36:00496782708e | 25 | int main() { |
ciror00 | 36:00496782708e | 26 | printf("\n----- Ejemplo de test REST Client -----\n"); |
ciror00 | 36:00496782708e | 27 | // Conéctese a la red con la interfaz de red predeterminada |
ciror00 | 36:00496782708e | 28 | NetworkInterface* network = connect_to_default_network_interface(); |
ciror00 | 36:00496782708e | 29 | if (!network) { |
ciror00 | 36:00496782708e | 30 | printf("No se puede conectar a la red\n"); |
ciror00 | 36:00496782708e | 31 | return 1; |
ciror00 | 36:00496782708e | 32 | } |
ciror00 | 36:00496782708e | 33 | |
ciror00 | 36:00496782708e | 34 | // Se realiza la solicitud GET |
ciror00 | 36:00496782708e | 35 | { |
ciror00 | 36:00496782708e | 36 | // De forma predeterminada, el cuerpo se analiza y almacena automáticamente en un búfer, esto es una gran cantidad de memoria. |
ciror00 | 36:00496782708e | 37 | // Para recibir una respuesta fragmentada, pase una devolución de llamada como último parámetro al constructor. |
ciror00 | 36:00496782708e | 38 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://192.168.1.75:3000/mediciones"); |
ciror00 | 36:00496782708e | 39 | |
ciror00 | 36:00496782708e | 40 | HttpResponse* get_res = get_req->send(); |
ciror00 | 36:00496782708e | 41 | if (!get_res) { |
ciror00 | 36:00496782708e | 42 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
ciror00 | 36:00496782708e | 43 | return 1; |
ciror00 | 36:00496782708e | 44 | } |
ciror00 | 36:00496782708e | 45 | |
ciror00 | 36:00496782708e | 46 | printf("\n----- HTTP GET response -----\n"); |
ciror00 | 36:00496782708e | 47 | response(get_res); |
ciror00 | 36:00496782708e | 48 | |
ciror00 | 36:00496782708e | 49 | delete get_req; |
ciror00 | 36:00496782708e | 50 | } |
ciror00 | 36:00496782708e | 51 | |
ciror00 | 36:00496782708e | 52 | // Solicitud POST |
ciror00 | 36:00496782708e | 53 | { |
ciror00 | 36:00496782708e | 54 | HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://192.168.1.75:3000/mediciones"); |
ciror00 | 36:00496782708e | 55 | post_req->set_header("Content-Type", "application/json"); |
ciror00 | 36:00496782708e | 56 | |
ciror00 | 36:00496782708e | 57 | const char body[] = "{\"uuid\":\"12345\",\"valor\":12.34}"; |
ciror00 | 36:00496782708e | 58 | |
ciror00 | 36:00496782708e | 59 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
ciror00 | 36:00496782708e | 60 | if (!post_res) { |
ciror00 | 36:00496782708e | 61 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
ciror00 | 36:00496782708e | 62 | return 1; |
ciror00 | 36:00496782708e | 63 | } |
ciror00 | 36:00496782708e | 64 | |
ciror00 | 36:00496782708e | 65 | printf("\n----- HTTP POST response -----\n"); |
ciror00 | 36:00496782708e | 66 | response(post_res); |
ciror00 | 36:00496782708e | 67 | |
ciror00 | 36:00496782708e | 68 | delete post_req; |
ciror00 | 36:00496782708e | 69 | } |
ciror00 | 36:00496782708e | 70 | |
ciror00 | 36:00496782708e | 71 | wait(osWaitForever); |
ciror00 | 36:00496782708e | 72 | } |
ciror00 | 36:00496782708e | 73 | |
ciror00 | 36:00496782708e | 74 | //=====[Implementations of public functions]=================================== |
ciror00 | 36:00496782708e | 75 | |
ciror00 | 36:00496782708e | 76 | void response(HttpResponse* res) { |
ciror00 | 36:00496782708e | 77 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
ciror00 | 36:00496782708e | 78 | |
ciror00 | 36:00496782708e | 79 | printf("Headers:\n"); |
ciror00 | 36:00496782708e | 80 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ciror00 | 36:00496782708e | 81 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
ciror00 | 36:00496782708e | 82 | } |
ciror00 | 36:00496782708e | 83 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
ciror00 | 36:00496782708e | 84 | } |
ciror00 | 36:00496782708e | 85 | |
ciror00 | 36:00496782708e | 86 | #endif |