cancel
Showing results for 
Search instead for 
Did you mean: 

MBEDTLS: Trying to POST/PUT/GET a JSON

Luis Ber
Associate II

Hello everyone,

I'm using the STM32F769I-DISCO evaluation board to perform a SSL connection using the MBEDTLS and LWIP Middlewares.

I have found the example for this evaluation board:

https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32F769I-Discovery/Applications/mbedTLS

This is very helpful to me and I achieve to perform the following connections:

  1. Local connection between the STM32F7 board (client) and a PC (server). I perform a "GET / HTTP/1.0\n\n" from client to server.
  2. External connection between the STM32F7 board (client) and a external server (8.8.8.8, google for example). I perform a "GET / HTTP/1.0\n\n" from client to server.
  3. Now I'm trying to GET, PUT, POST a JSON but I cannot obtain a correct response from the server.

The server where I trying to POST the JSON is "reqbin.com".

The POST that I'm performing is the following:

"POST /echo/post/json HTTP/1.1\nHost: reqbin.com\nContent-Type: application/json\nContent-Length: 8\n\n{\"Id\":7}"

The functions that I'm using to write and read are the following:

/*
   * 6. Write the POST request
   */
  
  mbedtls_printf( "  > Write to server:" );
  
  sprintf((char *) buf, POST_REQUEST);
  len = strlen((char *) buf);
  
  while((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0)
  {
    if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
    {
      mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
      goto exit;
    }
  }
 
  len = ret;
  mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);
 
  /*
   * 7. Read the HTTP response
   */
   mbedtls_printf("  < Read from server:");
 
  do
  {
    len = sizeof( buf ) - 1;
    memset( buf, 0, sizeof( buf ) );
    ret = mbedtls_ssl_read( &ssl, buf, len );
 
    if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
    {
      continue;
    }
    
    if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
    {
      break;
    }
 
    if(ret < 0)
    {
      mbedtls_printf( "failed\n  ! mbedtls_ssl_read returned %d\n\n", ret );
      break;
    }
 
    if(ret == 0)
    {
      mbedtls_printf( "\n\nEOF\n\n" );
      break;
    }
 
    len = ret;
    mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  }
  while(1);

The response of the server isi always 403 Forbidden, and I don't know where is the error.

I'm not an expert in these topics and some help will be very useful for me. I'm not using any type of certification and I think it's not necessary.

Thank you in advance.

0 REPLIES 0