GR-MANGO sample using mbed-os library from my repository.

Dependencies:   mbed-http

Committer:
RyoheiHagimoto
Date:
Mon Oct 12 02:25:49 2020 +0000
Revision:
0:b4c1e32627f2
replaced mbed-os library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:b4c1e32627f2 1 /*******************************************************************************
RyoheiHagimoto 0:b4c1e32627f2 2 * DISCLAIMER
RyoheiHagimoto 0:b4c1e32627f2 3 * This software is supplied by Renesas Electronics Corporation and is only
RyoheiHagimoto 0:b4c1e32627f2 4 * intended for use with Renesas products. No other uses are authorized. This
RyoheiHagimoto 0:b4c1e32627f2 5 * software is owned by Renesas Electronics Corporation and is protected under
RyoheiHagimoto 0:b4c1e32627f2 6 * all applicable laws, including copyright laws.
RyoheiHagimoto 0:b4c1e32627f2 7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
RyoheiHagimoto 0:b4c1e32627f2 8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
RyoheiHagimoto 0:b4c1e32627f2 9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
RyoheiHagimoto 0:b4c1e32627f2 10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
RyoheiHagimoto 0:b4c1e32627f2 11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
RyoheiHagimoto 0:b4c1e32627f2 12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
RyoheiHagimoto 0:b4c1e32627f2 13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
RyoheiHagimoto 0:b4c1e32627f2 14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
RyoheiHagimoto 0:b4c1e32627f2 15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
RyoheiHagimoto 0:b4c1e32627f2 16 * Renesas reserves the right, without notice, to make changes to this software
RyoheiHagimoto 0:b4c1e32627f2 17 * and to discontinue the availability of this software. By using this software,
RyoheiHagimoto 0:b4c1e32627f2 18 * you agree to the additional terms and conditions found by accessing the
RyoheiHagimoto 0:b4c1e32627f2 19 * following link:
RyoheiHagimoto 0:b4c1e32627f2 20 * http://www.renesas.com/disclaimer
RyoheiHagimoto 0:b4c1e32627f2 21 *
RyoheiHagimoto 0:b4c1e32627f2 22 * Copyright (C) 2019 Renesas Electronics Corporation. All rights reserved.
RyoheiHagimoto 0:b4c1e32627f2 23 *******************************************************************************/
RyoheiHagimoto 0:b4c1e32627f2 24 #include "sample_select.h"
RyoheiHagimoto 0:b4c1e32627f2 25
RyoheiHagimoto 0:b4c1e32627f2 26 #if (SAMPLE_PROGRAM_NO == 13)
RyoheiHagimoto 0:b4c1e32627f2 27 // SAMPLE_PROGRAM_NO 13 : Ether HTTP sample
RyoheiHagimoto 0:b4c1e32627f2 28
RyoheiHagimoto 0:b4c1e32627f2 29 #if defined(TARGET_SEMB1402)
RyoheiHagimoto 0:b4c1e32627f2 30 #error "Ether is not supported."
RyoheiHagimoto 0:b4c1e32627f2 31 #endif
RyoheiHagimoto 0:b4c1e32627f2 32
RyoheiHagimoto 0:b4c1e32627f2 33 #include "mbed.h"
RyoheiHagimoto 0:b4c1e32627f2 34 #include "http_request.h"
RyoheiHagimoto 0:b4c1e32627f2 35 #include "EthernetInterface.h"
RyoheiHagimoto 0:b4c1e32627f2 36
RyoheiHagimoto 0:b4c1e32627f2 37 EthernetInterface eth;
RyoheiHagimoto 0:b4c1e32627f2 38
RyoheiHagimoto 0:b4c1e32627f2 39 void dump_response(HttpResponse* res) {
RyoheiHagimoto 0:b4c1e32627f2 40 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
RyoheiHagimoto 0:b4c1e32627f2 41
RyoheiHagimoto 0:b4c1e32627f2 42 printf("Headers:\n");
RyoheiHagimoto 0:b4c1e32627f2 43 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
RyoheiHagimoto 0:b4c1e32627f2 44 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
RyoheiHagimoto 0:b4c1e32627f2 45 }
RyoheiHagimoto 0:b4c1e32627f2 46 printf("\nBody (%ld bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
RyoheiHagimoto 0:b4c1e32627f2 47 }
RyoheiHagimoto 0:b4c1e32627f2 48
RyoheiHagimoto 0:b4c1e32627f2 49 int main() {
RyoheiHagimoto 0:b4c1e32627f2 50 NetworkInterface* network = &eth;
RyoheiHagimoto 0:b4c1e32627f2 51 SocketAddress address;
RyoheiHagimoto 0:b4c1e32627f2 52
RyoheiHagimoto 0:b4c1e32627f2 53 printf("\nConnecting...\n");
RyoheiHagimoto 0:b4c1e32627f2 54 int ret = network->connect();
RyoheiHagimoto 0:b4c1e32627f2 55 if (ret != 0) {
RyoheiHagimoto 0:b4c1e32627f2 56 printf("\nConnection error\n");
RyoheiHagimoto 0:b4c1e32627f2 57 return -1;
RyoheiHagimoto 0:b4c1e32627f2 58 }
RyoheiHagimoto 0:b4c1e32627f2 59 printf("Success\n\n");
RyoheiHagimoto 0:b4c1e32627f2 60 printf("MAC: %s\n", network->get_mac_address());
RyoheiHagimoto 0:b4c1e32627f2 61 printf("IP: %s\n" , NSAPI_ERROR_OK == network->get_ip_address(&address) ? address.get_ip_address() : "NULL");
RyoheiHagimoto 0:b4c1e32627f2 62 printf("Netmask: %s\n", NSAPI_ERROR_OK == network->get_netmask(&address) ? address.get_ip_address() : "NULL");
RyoheiHagimoto 0:b4c1e32627f2 63 printf("Gateway: %s\n", NSAPI_ERROR_OK == network->get_gateway(&address) ? address.get_ip_address() : "NULL");
RyoheiHagimoto 0:b4c1e32627f2 64
RyoheiHagimoto 0:b4c1e32627f2 65 // Do a GET request to httpbin.org
RyoheiHagimoto 0:b4c1e32627f2 66 {
RyoheiHagimoto 0:b4c1e32627f2 67 // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
RyoheiHagimoto 0:b4c1e32627f2 68 // To receive chunked response, pass in a callback as last parameter to the constructor.
RyoheiHagimoto 0:b4c1e32627f2 69 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
RyoheiHagimoto 0:b4c1e32627f2 70
RyoheiHagimoto 0:b4c1e32627f2 71 HttpResponse* get_res = get_req->send();
RyoheiHagimoto 0:b4c1e32627f2 72 if (!get_res) {
RyoheiHagimoto 0:b4c1e32627f2 73 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
RyoheiHagimoto 0:b4c1e32627f2 74 return 1;
RyoheiHagimoto 0:b4c1e32627f2 75 }
RyoheiHagimoto 0:b4c1e32627f2 76
RyoheiHagimoto 0:b4c1e32627f2 77 printf("\n----- HTTP GET response -----\n");
RyoheiHagimoto 0:b4c1e32627f2 78 dump_response(get_res);
RyoheiHagimoto 0:b4c1e32627f2 79
RyoheiHagimoto 0:b4c1e32627f2 80 delete get_req;
RyoheiHagimoto 0:b4c1e32627f2 81 }
RyoheiHagimoto 0:b4c1e32627f2 82
RyoheiHagimoto 0:b4c1e32627f2 83 // POST request to httpbin.org
RyoheiHagimoto 0:b4c1e32627f2 84 {
RyoheiHagimoto 0:b4c1e32627f2 85 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
RyoheiHagimoto 0:b4c1e32627f2 86 post_req->set_header("Content-Type", "application/json");
RyoheiHagimoto 0:b4c1e32627f2 87
RyoheiHagimoto 0:b4c1e32627f2 88 const char body[] = "{\"hello\":\"world\"}";
RyoheiHagimoto 0:b4c1e32627f2 89
RyoheiHagimoto 0:b4c1e32627f2 90 HttpResponse* post_res = post_req->send(body, strlen(body));
RyoheiHagimoto 0:b4c1e32627f2 91 if (!post_res) {
RyoheiHagimoto 0:b4c1e32627f2 92 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
RyoheiHagimoto 0:b4c1e32627f2 93 return 1;
RyoheiHagimoto 0:b4c1e32627f2 94 }
RyoheiHagimoto 0:b4c1e32627f2 95
RyoheiHagimoto 0:b4c1e32627f2 96 printf("\n----- HTTP POST response -----\n");
RyoheiHagimoto 0:b4c1e32627f2 97 dump_response(post_res);
RyoheiHagimoto 0:b4c1e32627f2 98
RyoheiHagimoto 0:b4c1e32627f2 99 delete post_req;
RyoheiHagimoto 0:b4c1e32627f2 100 }
RyoheiHagimoto 0:b4c1e32627f2 101
RyoheiHagimoto 0:b4c1e32627f2 102 ThisThread::sleep_for(osWaitForever);
RyoheiHagimoto 0:b4c1e32627f2 103 }
RyoheiHagimoto 0:b4c1e32627f2 104
RyoheiHagimoto 0:b4c1e32627f2 105 #endif