Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 //=====[Libraries]============================================================= 00002 00003 // Se incluye como parte de la la clase HTTP 00004 #include "select-demo.h" 00005 #if DEMO == MAIN 00006 00007 #include "mbed.h" 00008 #include "http_request.h" 00009 #include "network-helper.h" 00010 #include "mbed_mem_trace.h" 00011 00012 #include <stdio.h> 00013 #include <string.h> 00014 00015 //=====[Declaration and initialization of public global objects]=============== 00016 00017 //=====[Declaration and initialization of public global variables]============= 00018 00019 //=====[Declarations (prototypes) of public functions]========================= 00020 00021 void response(HttpResponse* res); 00022 00023 //=====[Main function, the program entry point after power on or reset]======== 00024 00025 int main() { 00026 printf("\n----- Ejemplo de test REST Client -----\n"); 00027 // Conéctese a la red con la interfaz de red predeterminada 00028 NetworkInterface* network = connect_to_default_network_interface(); 00029 if (!network) { 00030 printf("No se puede conectar a la red\n"); 00031 return 1; 00032 } 00033 00034 // Se realiza la solicitud GET 00035 { 00036 // De forma predeterminada, el cuerpo se analiza y almacena automáticamente en un búfer, esto es una gran cantidad de memoria. 00037 // Para recibir una respuesta fragmentada, pase una devolución de llamada como último parámetro al constructor. 00038 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://192.168.1.75:3000/mediciones"); 00039 00040 HttpResponse* get_res = get_req->send(); 00041 if (!get_res) { 00042 printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00043 return 1; 00044 } 00045 00046 printf("\n----- HTTP GET response -----\n"); 00047 response(get_res); 00048 00049 delete get_req; 00050 } 00051 00052 // Solicitud POST 00053 { 00054 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://192.168.1.75:3000/mediciones"); 00055 post_req->set_header("Content-Type", "application/json"); 00056 00057 const char body[] = "{\"uuid\":\"12345\",\"valor\":12.34}"; 00058 00059 HttpResponse* post_res = post_req->send(body, strlen(body)); 00060 if (!post_res) { 00061 printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00062 return 1; 00063 } 00064 00065 printf("\n----- HTTP POST response -----\n"); 00066 response(post_res); 00067 00068 delete post_req; 00069 } 00070 00071 wait(osWaitForever); 00072 } 00073 00074 //=====[Implementations of public functions]=================================== 00075 00076 void response(HttpResponse* res) { 00077 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00078 00079 printf("Headers:\n"); 00080 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00081 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00082 } 00083 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00084 } 00085 00086 #endif
Generated on Thu Jul 28 2022 01:28:18 by
1.7.2