10 Replies Latest reply on Mar 1, 2018 2:44 AM by GauravS_31

    How to make a HTTP2 GET request?

    jackson_lv

      HI , I am a starter with http2. Now I want to make a http2 get request with MFI SDK4.0.1 .The request is like this:

      :method = GET

      :scheme = https

      :path = /{{API version}}/directives

      authorization = Bearer {{YOUR_ACCESS_TOKEN}}

       

      the wirte header and write request is right ,but the http client will disconnect immediately after http request flush.It is normal?

       

      Below is how I creat a get request:

      static const char* request_uris[] =

      {

        [0] = "/get"

      };

          WPRINT_APP_INFO( ( "Resolving IP address of HTTPS server\n" ) );
          wiced_hostname_lookup(CONNECTION_SERVER_HOST, &ip_address, DNS_TIMEOUT_MS);
          WPRINT_APP_INFO( ( "%s is at %u.%u.%u.%u\n", CONNECTION_SERVER_HOST,
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 24),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 16),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 8),
                                                       (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 0) ) );

       

       

          /* Initialize the root CA certificate */
          result = wiced_tls_init_root_ca_certificates( amazon_root_ca_certificate, strlen( amazon_root_ca_certificate ) );
          if ( result != WICED_SUCCESS )
          {
              WPRINT_APP_INFO( ( "Error: Root CA certificate failed to initialize: %u\n", result) );
          }
          WPRINT_APP_INFO(("Root CA certificate successed to initialize!\n"));

       

       

          sprintf(https_header,":method = GET\r\n:scheme = https\r\n:path = /v20160207/directives\r\nauthorization = Bearer %s\r\n",access_token);
          WPRINT_APP_INFO(("\n%s\n\n", https_header));

       

       

          result = http_client_init( &client, WICED_STA_INTERFACE, event_handler, NULL );

       

       

          if (result == WICED_SUCCESS)
      {
          WPRINT_APP_INFO( ( "http inited success  %d\n", result ) );
      }
      else WPRINT_APP_INFO( ( "http failed to init %d\n", result ) );
          /* configure HTTP client parameters */
          client_configuration.flag = HTTP_CLIENT_CONFIG_FLAG_SERVER_NAME | HTTP_CLIENT_CONFIG_FLAG_MAX_FRAGMENT_LEN;
          client_configuration.server_name = (uint8_t*)CONNECTION_SERVER_HOST;
          client_configuration.max_fragment_length = TLS_FRAGMENT_LENGTH_4096;
          http_client_configure(&client, &client_configuration);

       

       

          result = http_client_connect( &client, (const wiced_ip_address_t*)&ip_address, SERVER_PORT, HTTP_USE_TLS, CONNECT_TIMEOUT_MS );
          if (result == WICED_SUCCESS)
          {
          WPRINT_APP_INFO( ( "http connected %d\n", result ) );

       

       

       

       

              header[0].field        = HTTP_HEADER_HOST;
              header[0].field_length = sizeof( HTTP_HEADER_HOST) - 1;
              header[0].value        = CONNECTION_SERVER_HOST;
              header[0].value_length = sizeof( CONNECTION_SERVER_HOST ) - 1;

       

      char *http_get_length_string = malloc(10);

       

           itoa(strlen(https_header),http_get_length_string,10);


      header[2].field        = HTTP_HEADER_CONTENT_TYPE;
      header[2].field_length = sizeof( HTTP_HEADER_CONTENT_TYPE ) - 1;
      header[2].value        = "application/x-www-form-urlencoded";
          header[2].value_length = sizeof( "application/x-www-form-urlencoded" ) - 1;
              header[1].field        = HTTP_HEADER_CONTENT_LENGTH;
      header[1].field_length = sizeof( HTTP_HEADER_CONTENT_LENGTH ) - 1;
      header[1].value        = http_get_length_string;
      header[1].value_length = strlen( http_get_length_string );

          header[3].field        = "Connection: ";
          header[3].field_length = sizeof("Connection: ") - 1;
          header[3].value        = "Keep-Alive";
          header[3].value_length = sizeof( "Keep-Alive" ) - 1;
      http_request_init( &requests[0], &client, HTTP_GET, request_uris[0], HTTP_2 );
      for(uint8_t i=0;i<4;i++)
      {
      http_request_write_header( &requests[0], &header[i], 1 );
      WPRINT_APP_INFO( ( "http_request_write_header %d result : %d \n" ,i,result) );
      }
      http_request_write_end_header( &requests[0] );
      http_request_write(&requests[0],(uint8_t *)https_header,sizeof(https_header)-1);//write body
      WPRINT_APP_INFO( ( "http_request_write result : %d \n" ,result) );
      http_request_write_end_header( &requests[0] );
      http_request_flush( &requests[0] );
          }
          else WPRINT_APP_INFO( ( "http failed to connect %d\n", result ) );