This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C1294NCPDT: RTOS/TI-RTOS HTTP client POST body is empty

Part Number: TM4C1294NCPDT

Tool/software: Code Composer Studio

Hi,

I'm making iot system with Ethernet.

Everything is  working well but only issue is, request's body is empty.

this is log from server. first brace is request object's header, and second brace is request object's body.

As you can see, header is good, but body is empty.

{
  host: '*host server address*',
  'content-type': 'application/x-www-form-urlencoded',
  'content-length': '14'
}

{}

this is my code

Void sendPostRequest()
{
  bool moreFlag = false;

      char responsedata[64];
      char postdata[] = "test=1&test2=2";
      int ret;
      int len;
      struct sockaddr_in addr;

      HTTPCli_Struct cli;
      HTTPCli_Field fields[3] = {
          { HTTPStd_FIELD_NAME_HOST, HOSTNAME },
          { NULL, NULL }
      };

      System_printf("Sending a HTTP GET request to '%s'\n", HOSTNAME);
      System_flush();

      HTTPCli_construct(&cli);

      HTTPCli_setRequestFields(&cli, fields);

      ret = HTTPCli_initSockAddr((struct sockaddr *)&addr, HOSTNAME, 0);
      if (ret == 0) {
        System_printf("httpTask: address resolution done\n", ret);
      }

      ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, NULL);
      if (ret == 0) {
        System_printf("httpTask: connect done\n", ret);
      }

      ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);
      if (ret == 0) {
        System_printf("httpTask: send done\n", ret);
      }

      ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_TYPE, "application/x-www-form-urlencoded", false);
      if (ret == 0) {
        System_printf("httpTask: send field done\n", ret);
      }
      ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, "14", true);
      if (ret == 0) {
        System_printf("httpTask: send field done\n", ret);
      }

      ret = HTTPCli_sendRequestBody(&cli, postdata, strlen(postdata));
      if (ret == 0) {
        System_printf("httpTask: send body done\n", ret);
      }

      ret = HTTPCli_getResponseStatus(&cli);
      if (ret != HTTPStd_OK) {
        System_printf("httpTask: cannot get status\n", ret);
      }

      System_printf("HTTP Response Status Code: %d\n", ret);

      ret = HTTPCli_getResponseField(&cli, responsedata, sizeof(responsedata), &moreFlag);
      if (ret != HTTPCli_FIELD_ID_END) {
        System_printf("httpTask: response field processing failed\n", ret);
      }

      len = 0;
      do {
          ret = HTTPCli_readResponseBody(&cli, responsedata, sizeof(responsedata), &moreFlag);
          if (ret < 0) {
            System_printf("httpTask: response body processing failed\n", ret);
          }

          len += ret;
      } while (moreFlag);

      System_printf("Recieved %d bytes of payload\n", len);
      System_flush();

      HTTPCli_disconnect(&cli);
      HTTPCli_destruct(&cli);
}

I checked whole request object, but I couldn't find data I sent.

  • Hi,

    Which version of TI-RTOS are you using?

    Can you pot your example's output from running the above code?

    Thanks,
    Gerardo

  • Hi,

    I'm using tirtos_tivac_2_6_01_14.

    And this is console output.

    [CORTEX_M4_0] Using MAC address in flash
    Service Status: DHCPC    : Enabled  :          : 000
    Service Status: DHCPC    : Enabled  : Running  : 000
    Network Added: If-1:192.168.31.249
    addrAddHook: Create httpTask OK
    Service Status: DHCPC    : Enabled  : Running  : 017
    Sending a HTTP GET request to 'http://192.168.31.133:3000'
    : send field done
    httpTask: send field done
    httpTask: send body done
    HTTP Response Status Code: 200
    Recieved 5 bytes of payload
    

    I can't find why some System_printf message missing(like one in initSockAddr, connect, sendRequest), and some sentence cut(like line 8), but I checked all function return 0 or other success code.

    5bytes of payload is check message from server and it is correct

  • I changed Content-type to 'application/json' and format of data to json, it work!

    Maybe I missed something when use 'application/x-www-form-urlencoded'.

    I changed data to

    char postdata[] = "{\"test1\":1,\"test2\":2}";