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.

CC3235S: HTTP client GET request

Part Number: CC3235S
Other Parts Discussed in Thread: UNIFLASH

Hi,

I am using HTTP client example from SDK for GET request. When i use www.google.com as HOSTNAME it works fine and i get google home page html in response to GET request. But when i replace same by my server link, I get response code as 404. However, If I use same server link in PC based software like Postman then I can see my server perfectly gives expected response to GET request. Below is the code snippet. Any guidance on this issue?

--------------------

#define HOSTNAME "my server link"
#define REQUEST_URI "/"
#define USER_AGENT "HTTPClient (ARM; TI-RTOS)"
#define HTTP_MIN_RECV (256)

HTTPClient_extSecParams secparams;

void functionA(void) {

HTTPClient_Handle httpClientHandle;
int16_t statusCode;
httpClientHandle = HTTPClient_create(&statusCode,0);
if(statusCode < 0)
{
Display_printf(display, 0, 0, "httpTask: creation of http client handle failed:%d ",
statusCode);

}

Display_printf(display, 0, 0, "httpTask: client creation ok\n");

ret =
HTTPClient_setHeader(httpClientHandle,
HTTPClient_HFIELD_REQ_USER_AGENT, //HTTPClient_HFIELD_REQ_CONTENT_TYPE,
USER_AGENT,strlen(USER_AGENT)+1,
HTTPClient_HFIELD_PERSISTENT);
if(ret < 0)
{
Display_printf(display, 0, 0, "httpTask: setting request header failed:%d ",
ret);
}

Display_printf(display, 0, 0, "httpTask: set header ok\n");

ret = HTTPClient_connect(httpClientHandle,HOSTNAME,&secparams,0); //&secparams is necessary else this api does not return
if(ret < 0)
{
Display_printf(display, 0, 0, "httpTask: connect failed:%d ",
ret);
}

Display_printf(display, 0, 0, "httpTask: client connect ok\n");

ret =
HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,REQUEST_URI,
NULL,0, //sizeof(msg),
0);
if(ret < 0)
{
Display_printf(display, 0, 0, "httpTask: send failed:%d ",
ret);
}

Display_printf(display, 0, 0, "httpTask: send request ok\n");

if(ret != HTTP_SC_OK)
{
Display_printf(display, 0, 0, "httpTask: cannot get status:%d ",
ret);
}

Display_printf(display, 0, 0, "HTTP Response Status Code: %d\n ",
ret);

len = 0;
do
{
ret = HTTPClient_readResponseBody(httpClientHandle, data, sizeof(data),
&moreDataFlag);
if(ret < 0)
{
Display_printf(display, 0, 0, "httpTask: response body processing failed:%d ",
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);

ret = HTTPClient_disconnect(httpClientHandle);
if(ret < 0)
{
Display_printf(display, 0, 0, "httpTask: disconnect failed:%d ",
ret);
}

HTTPClient_destroy(httpClientHandle);

}

  

Thanks,

Pradeep

  • Hi Pradeep,

    it seems you are trying to connect to your server in a secure connection as you are setting "ret = HTTPClient_connect(httpClientHandle,HOSTNAME,&secparams,0);"

    "secparams" needs to point to the name of your server's certificate that is saved in flash( the certificate itself can be added to flash via uniflash or by copying the certificate into the user files in your directory).

    For more info on how to use certificates refer to this users guide:

    https://www.ti.com/lit/ug/swpu332a/swpu332a.pdf?ts=1679242410366&ref_url=https%253A%252F%252Fwww.google.com%252F

    then set secparams:

    HTTPClient_extSecParams extSecParams =
    {
    NULL,/* Private key */
    NULL,/* Client certificate */
    "my server link certificate" /* Root CA */
    };

    in httpget.c

    make sure the correct time is set as well, this is used to verify the certificates.

    in addition make sure that you are able to ping your server.

    best regards,

    Avi

  • Hi Avi,

    Thankyou for your reply.

    The error was incorrect definition of REQUEST_URI. 

    The complete link was https://myserver.com/f1/f2/f3. In this REQUEST_URI  should be /f1/f2/f3 while HOSTNAME should be myserver.com. After doing these modifications it worked.

    On the same line, now I am implemented POST request. I am getting error 400 in response to POST request. Do you have any example code for POST request?

    Thanks,

    Pradeep

  • hey Pradeep,

    glad to hear that the previous issue was solved.

    there is no designated example for this however if you take a look at demos/mqtt_client/ifmod/ota_vendor_dropbox.c lines 316 and 361 

    in OTA_DROPBOX_getDownloadLink() function you there is an example using HTTP_METHOD_POST.
    best regards,
    Avi
  • Hi Avi,

    I do not see this file in my SDK, probably because I am using SDK version 4.1. In which version of SDK can i find this file? As I am using Azure SDK which is present in Simplelink SDK 4.10, I don;t want to install other version of SimpleLink SDK. Can you please do me a favour of sending this file or copy pasting the relevant code here?

    Thanks,

    Pradeep

  • int32_t OTA_DROPBOX_getDownloadLink(FileServerParams_t *pServerParams){
        int32_t ret;
        gdl_buffer_t* buff;

        HTTPClient_Handle httpClientHandle = NULL;
        int16_t jsonSize;
        uint16_t linkSize;
        Json_Handle listFolderTemplateHandle, getLinkTemplateHandle;
        Json_Handle getLinkObjectHandle;


        // Allocate the buffer and the output URL on the heap.
        buff = (gdl_buffer_t*) calloc (1, sizeof(gdl_buffer_t));

        if(buff)
        {
            LOG_INFO("*** Attempting DROPBOX getDownloadLink ***");

            // Create template for parsing the list_folder request response.
            ret = Json_createTemplate(&listFolderTemplateHandle, LIST_FOLDER_RES_JSON_TEMPLATE,
                                      strlen(LIST_FOLDER_RES_JSON_TEMPLATE));
            if(ret < 0){
                LOG_ERROR("create JSON template failed: %d", ret);
            }
            else
            {
                ret = HTTP_setupConnection(&httpClientHandle, HOSTNAME, NULL, NULL, DROPBOX_ROOT_CA, ALT_DROPBOX_ROOT_CA, 0);
            }

            if(ret == 0)
            {
                char *pReqURI = LIST_FOLDER_REQ_URI;

                // Setup the first HTTP POST request with the list_folder API call.
                strcpy(buff->data, LIST_FOLDER_REQ_DATA);

                do {
                    // Send HTTP POST request (LIST_FOLDER)
                    LOG_DEBUG("=== Sending POST Request ===\r\n uri:%s\r\n data:%s", pReqURI, buff->data);
                    ret = HTTP_setHeaders(httpClientHandle, NULL, APP_TOKEN, CONTENT_TYPE, HTTPClient_HFIELD_NOT_PERSISTENT);
                    if(ret == 0)
                    {
                        ret = HTTPClient_sendRequest(httpClientHandle,
                                                     HTTP_METHOD_POST,pReqURI,
                                                     buff->data,strlen(buff->data),
                                                     0);
                        LOG_DEBUG("HTTP Response Status Code: %d", ret);
    #ifdef OATH2_SUPPORT
                        if(ret == HTTP_SC_UNAUTHORIZED)
                        {
                            GetTempToken(httpClientHandle, buff->data);
                        } else
    #endif
                        if(ret < 0) {

                            LOG_ERROR("post request failed: %d", ret);
                            free(buff);
                            return (ret);
                        }
                    }
                    // Read JSON Response from POST request into data buffer
                    ret = ReadHTTPResponse(httpClientHandle, buff->data);
                    if(ret < 0) { // Negative on error
                        free(buff);
                    }
                    else
                    {
                        buff->data[ret] = 0;

                        ret = ParseListFolderResponse(listFolderTemplateHandle, buff->data, ret);

                        // Setup the next HTTP POST request with the list_folder/continue API call.
                        pReqURI =  LIST_FOLDER_CONT_REQ_URI;
                    }
                } while(ret == D_KEEP_LOOKING_FOR_NEW_VERSION); // Only continue when there are more files in the folder to check.

                if(ret == OTA_VENDOR_NO_NEW_IMAGE_FOUND)
                {
                    LOG_INFO("*** No new image was found!!! ***");
                }
                else if(ret == OTA_VENDOR_NEW_IMAGE_FOUND)
                {
                    // Send HTTP POST request (GET_LINK)
                    LOG_DEBUG("\n\r=== Sending POST Request ===\r\n uri:%s\r\n data:%s", GET_LINK_REQ_URI, buff->data);
                    ret = HTTP_setHeaders(httpClientHandle, NULL, APP_TOKEN, CONTENT_TYPE, HTTPClient_HFIELD_NOT_PERSISTENT);
                    if(ret == 0)
                    {
                        ret = HTTPClient_sendRequest(httpClientHandle,
                                                     HTTP_METHOD_POST,GET_LINK_REQ_URI,
                                                     buff->data,strlen(buff->data),
                                                     0);
                        if(ret < 0) {
                            LOG_ERROR("post request failed: %d", ret);
                        }
                        else
                        {
                            LOG_DEBUG("HTTP Response Status Code: %d", ret);

                            // Read JSON Response from POST request into data buffer
                            LOG_DEBUG("Reading JSON\r\n");
                            memset(buff->data, 0, MAX_BUFF_DATA_LEN);
                            jsonSize = ReadHTTPResponse(httpClientHandle, buff->data);
                            if(jsonSize < 0) {
                                ret = jsonSize;
                            }
                            else
                            {
                                // Create template for parsing the list_folder request response.
                                ret = Json_createTemplate(&getLinkTemplateHandle, GET_LINK_RES_JSON_TEMPLATE,
                                                          strlen(GET_LINK_RES_JSON_TEMPLATE));
                                if(ret < 0) {
                                    LOG_ERROR("create JSON template failed: %d", ret);
                                }
                                else
                                {
                                    // Setup the JSON object to read entries
                                    ret = Json_createObject(&getLinkObjectHandle, getLinkTemplateHandle, 0);
                                    if(ret < 0) {
                                        LOG_ERROR("create JSON object failed: %d", ret);
                                    }
                                    else
                                    {
                                        ret = Json_parse(getLinkObjectHandle, buff->data, jsonSize);
                                        if(ret < 0) {
                                            LOG_ERROR("parse JSON failed: %d", ret);
                                        }
                                        else
                                        {
                                            // Get the download link size from the JSON response.
                                            ret = Json_getValue(getLinkObjectHandle, "\"link\"",
                                                                NULL, &linkSize);
                                            if(ret < 0) {
                                                LOG_ERROR("JSON getValue (size of link) failed: %d", ret);
                                            }
                                            else
                                            {

                                                // Get the download link from the JSON response.
                                                memset(buff->data, 0, MAX_BUFF_DATA_LEN);
                                                ret =
                                                        Json_getValue(getLinkObjectHandle, "\"link\"",
                                                                      buff->data, NULL);
                                                if(ret < 0) {
                                                    LOG_ERROR("JSON getValue (link) failed: %d", ret);
                                                }
                                                else
                                                {
                                                    linkSize = strlen(buff->data);
                                                    memcpy(pServerParams->URL, buff->data, linkSize);
                                                    pServerParams->URL[linkSize] = '\0';
                                                    pServerParams->pVersion = NULL; /* NULL means that the version will be compared again
                                                       in OTA_IF against the timestamp at the beginning of the
                                                       tar file (which is part of the filename).
                                                       In case the version should be something else, make sure
                                                       allocate the space for it (and to free it after the
                                                       downloadImage call returns). */
                                                    pServerParams->pClientCert = NULL;
                                                    pServerParams->pPrivateKey = NULL;
                                                    pServerParams->pRootCa1 = DROPBOX_ROOT_CA;
                                                    pServerParams->pRootCa2 = ALT_DROPBOX_ROOT_CA;

                                                    LOG_INFO("*** New Image Found: (URL: %s) ***", pServerParams->URL);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // Close the HTTP connection and destroy the associated object.
                if(HTTPClient_disconnect(httpClientHandle) < 0) {
                    LOG_ERROR("http disconnect failed: %d", ret);
                }
                if(HTTPClient_destroy(httpClientHandle) < 0) {
                    LOG_ERROR("destroy failed: %d", ret);
                }
            }
            free(buff);

        }
        return (ret);
    }