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.

LAUNCHXL-CC3235SF: HTTPClient_Handle httpClientHandle; is getting corrupted

Part Number: LAUNCHXL-CC3235SF

Hi Ti Experts,

I have written a code where I'm performing two operations i.e HTTP POST and HTTP GET. After executing the first execution i.e. HTTP Post I have observed that when I call the function HTTPClient_destroy(httpClientHandle);

I saw that httpClientHandle is not freed it still contains the data. But when I call the second function HTTP GET. While destroying the HTTP Client my application is receiving the fault. 

According to my observation what I have observed is in the first function i.e in the POST method as the client is not destroyed it is leading to a fault in the GET request. Can anyone please say why it is unable to free the client handle? I have run the same code in the empty project it is working but when I'm integrating my code in the task it is getting a fault. 

I have given all the details of what issue I'm facing. Please assist me to resolve my query.

  • Hi Manish,

    What return value are you getting from HTTPClient_destroy?

    Why are you destroying the client instance when you intend to use it again later for GET? A code snippet may be helpful here.

    Best regards,

    Sarah

  • Hello Sarah,

    The reason why I'm destroying the HTTP Client instance is that I need to POST in the different hostname and do the GET request from the different hostname. If I won't destroy how will I be able to perform my task?

    After doing the POST request I need to disconnect and destroy the HTTP client instance right? When I do that I don't see that the HTTP Client instance is destroyed it still contains the same values basically when you destroy, you'll find the free function which is doing the job so how can I destroy the client instance. I have doubtsFreeRTOSConfig.h the I need to fix my FreeRTOSConfig.h file. For your instance, I'll attach my FreeRTOSConfig.h file.

  • Hi Manish,

    The hostname is passed at HTTPClient_connect, so you only have to disconnect. You do not need to destroy the client instance.

    You should also be able to create multiple client instances (one for each hostname), since the HTTP library works on top of sockets. Be sure to define two separate httpClientHandles.

    HTTPClient_destoy frees the client instance, but that does not necessarily mean the contents are zeroed out. Freeing the memory makes it available for reuse.

    Best regards,

    Sarah

  • Hi Sarah,

    Let me try out this.

  • HI Sarah,

    I get the error when I clear the request header for HTTP Client in the following code provided in httpclient.c

    static void clearReqHeaders(HTTPClient_CB *cli, bool persistent)
    {
    Req_HField *temp ,*head;

    if (persistent)
    {
    head = cli->reqHFieldPers;
    }
    else
    {
    head = cli->reqHField;
    }
    while (head != NULL)
    {
    temp = head;
    head = head->Next;                                  // I get the fault exception here but before that it is returning -2006
    if (temp->CustomName)
    {
    free(temp->CustomName);
    }
    free(temp->Value);
    free(temp);
    }
    if (persistent)
    {
    cli->reqHFieldPers = NULL;
    }
    else
    {
    cli->reqHField = NULL;
    }
    }

    So the fault comes here ---- in httpclient.c file

     head = head->Next;                                  // I get the fault exception here but before that it is returning -2006

    But before this one more place I'm doing the POST request and there I'm disconnecting the HTTP request in that case it is not hanged. Why does it hang in the GET request, I'm unable to figure it out.

    Can you please suggest some answers?

    Here is the image in httpclient.c

    Here is the call stack image when I checked it out what is the issue

  • Hi,

    As you said, we should not destroy the HTTP client instance but when I have used the HTTP Client instance and called the destroy function instance of the HTTP client is getting corrupted and also I'm getting Bus Fault. 

  • Hi Manish,

    I don't understand what you are trying to do. Can you post the commands you are calling in your application? Also, what is return value from HTTPClient_destroy?

    Best regards,

    Sarah

  • Hi Sarah,

    There are two functions which I'm trying to call one is for POST request and the other is for GET Request here are the functions stated below:

    Below Function is HTTP Post Request. It is working as expected.

    long device_provision_http_API_request()
    {
    unsigned long HostIP;
    HTTPClient_Handle httpClientHandle;
    int16_t statusCode,ret;
    int32_t lRet;
    SlDateTime_t dt;
    long body_len;
    int16_t len = 0;
    bool moreDataFlag = false,json = false;
    char mac[13]={0},Udid[13]={0};
    
    httpClientHandle = HTTPClient_create(&statusCode,0);
    
    lRetVal = HTTPClient_connect(httpClientHandle,HOSTNAME,0,0);
    
    ret = HTTPClient_setHeaderByName(httpClientHandle, HTTPClient_REQUEST_HEADER_MASK, "x-api-key", TOKEN, strlen(TOKEN), HTTPClient_HFIELD_PERSISTENT);
    
    stMqttConfig.latitude[16] = stMqttConfig.longitude[16] = NULL;
    
    memset(stGateway.Json_Buffer,NULL,sizeof(stGateway.Json_Buffer));
    
    body_len = sprintf(Json_Buffer, PROVISION_DATA,ThingName,mac,Udid,latitude,longitude);
    ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_POST,HOSTNAME,
    (char const *)Json_Buffer,body_len,
    0);
    if(ret == HTTP_SC_OK)
    {
    json = true;
    }
    else
    {
    json = false;
    }
    
    len = 0;
    do
    {
    ret = HTTPClient_readResponseBody(httpClientHandle, Firmware_Buffer, sizeof(Firmware_Buffer),
    &moreDataFlag);
    if(ret < 0)
    {
    //printError("httpTask: response body processing failed", ret);
    }
    //Display_printf(display, 0, 0, "%.*s \r\n",ret,data);
    len += ret;
    }
    while(moreDataFlag);
    
    //Display_printf(display, 0, 0, "Received %d bytes of payload\n", len);
    
    if(json == true)
    {
    lRetVal = ParseJSONData(Firmware_Buffer);
    if(lRetVal < 0)
    {
    return -1;
    }
    }
    ret = HTTPClient_disconnect(httpClientHandle);
    if(ret < 0)
    {
    while(1);
    }
    HTTPClient_destroy(httpClientHandle);
    }

    Below the function is the HTTP_GET request. Here when I'm trying to disconnect the HTTP Client instance using this API 

    HTTPClient_disconnect(&httpClientHandle);

    I can see in the watch window it has the same data and address assigned to it. But when disconnect API is called HTTP Client instance is passed to disconnect there it is assigned to the structure value is changed and also the pointer which has the address of httpClientHandle is pointing to somewhere else not pointing to httpClientHandle. This leads to Bus Fault says BAFR has occurred how I can figure it out.

    void http_get_request()
    
    {
    
    HTTPClient_Handle httpClientHandle;
    
    httpClientHandle = HTTPClient_create(&statusCode,0);
    if(statusCode < 0)
    {
    //DBG_MSG("Failed\r\n");
    return -1;
    }
    statusCode = HTTPClient_connect(httpClientHandle,HOSTNAME,0,0);
    if (statusCode < 0)
    {
    HTTPClient_disconnect(&httpClientHandle);
    HTTPClient_destroy(&httpClientHandle);
    
    }
    
    HTTPClient_setHeaderByName(httpClientHandle, HTTPClient_REQUEST_HEADER_MASK, "x-api-key", TOKEN, strlen(TOKEN), HTTPClient_HFIELD_PERSISTENT);
    
    lRetVal = HTTPClient_sendRequest(httpClientHandle, HTTP_METHOD_GET, HOSTNAME, NULL, 0, 0);
    
    do
    {
    ret = HTTPClient_readResponseBody(httpClientHandle, Firmware_Buffer, sizeof(Firmware_Buffer),
    &moreDataFlag);
    if(ret < 0)
    {
    return -1;
    }
    len += ret;
    }
    while(moreDataFlag);
    
    HTTPClient_disconnect(&httpClientHandle);
    HTTPClient_destroy(&httpClientHandle);
    
    }

    Eventually when I call the disconnect function it goes to httpclient.c file in there we have a socket close API

    ret = SlNetSock_close(cli->ssock);                    //return -2006.

    It is returned because in the above statement I told that httpClientHandle itself is corrupted so how can I fix that.

    Please ask if you need more clarifications from me.

  • Hi Manish,

    In your HTTP POST code, you are calling HTTPClient_destroy(httpClientHandle), which is correct.

    In your HTTP GET code, you are calling HTTPClient_destroy(&httpClientHandle), which is an incorrect parameter. Same with disconnect.

    Best regards,

    Sarah