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.

RTOS/EK-TM4C129EXL: TI-RTOS HTTPS POST SAMPLE

Part Number: EK-TM4C129EXL

Tool/software: TI-RTOS

Hi,

I'm working on TI_RTOS examples and I have implemented HTTPS GET example and I'm trying to implement HTTPS POST. 

Is there a sample example for HTTPS POST? If not what are the steps to make it to HTTPS POST?

and also is there a sample test server for HTTPS POST through which I can test my code?

I have made some changes in the code but i'm getting a send failed error.

Regards,

Deepak

  • Hi Deepak,

    This is the code you would use to make a POST:

    // Make HTTP 1.1 POST request
    //
    // Send request to the server:
    //
    // POST /index.html HTTP/1.1
    HTTPCli_sendRequest(&cli, HTTPStd_POST, "/index.html", true);
    
    // Send additional fields (optional)
    //
    // Content-Length: <length>
    // <blank line>
    HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, len, true);
    
    // Send request body
    //
    // <data>
    HTTPCli_sendRequestBody(&cli, data, strlen(data));

    You may be able to test with this server:
    https://docs.postman-echo.com/?version=latest#1eb1cf9d-2be7-4060-f554-73cd13940174

    Hope that helps,
    Gerardo

  • Hi Gerardo,

    I have modified the code as you said and coppied the cert from the "https://postman-echo.com" but i'm getting a send failed error.

    Here's the code that I have implemented 

    #define HOSTNAME "https://postman-echo.com"
    #define REQUEST_URI "/"
    #define CONTENT_TYPE "application/json"
    #define Authorization "Basic"
    #define USER_AGENT "HTTPCli (ARM; TI-RTOS)"

    Void httpsPostTask(UArg arg0, UArg arg1)
    {

    bool moreFlag = false;
    char data[64];
    int ret;
    int len;
    char CONTENT_LENGTH[3];
    struct sockaddr_in addr;

    TLS_Params tlsParams;
    TLS_Handle tls;

    //Data to be sent
    strcpy(data, "{\"val\": 10}");

    len = strlen(data);
    sprintf(CONTENT_LENGTH, "%d", len);

    System_printf("\nData: %s\n", data);
    System_printf("len(int): %d\n", len);
    System_printf("CONTENT_LENGTH: %s\n", CONTENT_LENGTH);


    HTTPCli_Params params;
    HTTPCli_Struct cli;
    HTTPCli_Field fields[5] = {
    { HTTPStd_FIELD_NAME_HOST, HOSTNAME },
    { HTTPStd_FIELD_NAME_AUTHORIZATION, Authorization },
    { HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE },
    { NULL, NULL }
    };

    startNTP();

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

    TLS_Params_init(&tlsParams);
    tlsParams.ca = ca;
    tlsParams.calen = calen;

    tls = TLS_create(TLS_METHOD_CLIENT_TLSV1_2, &tlsParams, NULL);
    if (!tls) {
    printError("httpsTask: TLS create failed", -1);
    }

    GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_2, 0);
    GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, GPIO_PIN_3);

    HTTPCli_construct(&cli);

    HTTPCli_setRequestFields(&cli, fields);

    ret = HTTPCli_initSockAddr((struct sockaddr *)&addr, HOSTNAME, 0);
    if (ret < 0) {
    printError("httpsTask: address resolution failed", ret);
    }

    HTTPCli_Params_init(&params);
    params.tls = tls;

    ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, &params);
    if (ret < 0) {
    printError("httpsTask: connect failed", ret);
    }

    ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);
    if (ret < 0) {
    printError("httpsTask: send failed", ret);
    }
    else
    {
    System_printf("sendRequest successful\n");
    }

    ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, CONTENT_LENGTH, false);
    ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE, true);

    if (ret < 0) {
    printError("httpTask: send failed", ret);
    }
    else {
    System_printf("sendField successful\n");
    }

    ret = HTTPCli_sendRequestBody(&cli, data, strlen(data));
    if (ret < 0) {
    printError("httpTask: Variable data couldn't be sent", ret);
    }
    else {
    System_printf("Data sent successfully\n");
    }

    ret = HTTPCli_getResponseStatus(&cli);
    if (ret != HTTPStd_OK) {
    printError("httpsTask: cannot get status", ret);
    }

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

    ret = HTTPCli_getResponseField(&cli, data, sizeof(data), &moreFlag);
    if (ret != HTTPCli_FIELD_ID_END) {
    printError("httpsTask: response field processing failed", ret);
    }

    len = 0;
    do {
    ret = HTTPCli_readResponseBody(&cli, data, sizeof(data), &moreFlag);
    if (ret < 0) {
    printError("httpsTask: response body processing failed", ret);
    }

    len += ret;
    } while (moreFlag);

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

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

    TLS_delete(&tls);

    }

    Do I need to make some other change for this to work?

    Regards,

    Deepak

  • Hi Deepak,

    Try posting to the /post URI by modifying the following

    #define REQUEST_URI "/post"

    If that still doesn't work can you tell me which of the HTTPCli calls is not working and what its return status is?

    BR,
    Gerardo

  • Hi Gerardo,

    I did. Still the same result. My HTTPS GET code is also not working which makes me feel like its an certificate issue. Can you send me the root certificate for postman if you have?

    Regards,

    Deepak

  • Hi Deepak,

    Update your certificate to the one found here:

    https://www.amazontrust.com/repository/AmazonRootCA1.pem

    Thanks,
    Gerardo

  • Hi Gerardo,

    This certificate is giving  me the same error but I got it working using another cert which I got from the certificate path from the browser.

    navigating to the root cert gives you the working cert.

     

    -----BEGIN CERTIFICATE-----
    MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJVUzEl
    MCMGA1UEChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMp
    U3RhcmZpZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDQw
    NjI5MTczOTE2WhcNMzQwNjI5MTczOTE2WjBoMQswCQYDVQQGEwJVUzElMCMGA1UE
    ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMpU3RhcmZp
    ZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwggEgMA0GCSqGSIb3
    DQEBAQUAA4IBDQAwggEIAoIBAQC3Msj+6XGmBIWtDBFk385N78gDGIc/oav7PKaf
    8MOh2tTYbitTkPskpD6E8J7oX+zlJ0T1KKY/e97gKvDIr1MvnsoFAZMej2YcOadN
    +lq2cwQlZut3f+dZxkqZJRRU6ybH838Z1TBwj6+wRir/resp7defqgSHo9T5iaU0
    X9tDkYI22WY8sbi5gv2cOj4QyDvvBmVmepsZGD3/cVE8MC5fvj13c7JdBmzDI1aa
    K4UmkhynArPkPw2vCHmCuDY96pzTNbO8acr1zJ3o/WSNF4Azbl5KXZnJHoe0nRrA
    1W4TNSNe35tfPe/W93bC6j67eA0cQmdrBNj41tpvi/JEoAGrAgEDo4HFMIHCMB0G
    A1UdDgQWBBS/X7fRzt0fhvRbVazc1xDCDqmI5zCBkgYDVR0jBIGKMIGHgBS/X7fR
    zt0fhvRbVazc1xDCDqmI56FspGowaDELMAkGA1UEBhMCVVMxJTAjBgNVBAoTHFN0
    YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xMjAwBgNVBAsTKVN0YXJmaWVsZCBD
    bGFzcyAyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5ggEAMAwGA1UdEwQFMAMBAf8w
    DQYJKoZIhvcNAQEFBQADggEBAAWdP4id0ckaVaGsafPzWdqbAYcaT1epoXkJKtv3
    L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D
    eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl
    xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp
    VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY
    WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=
    -----END CERTIFICATE-----

    thanks for your help.

    I still have a question, is it possible in TI-RTOS example to auto download the cert from a website? Right now we copy it and paste it in the code. I'm working on a product which will be released and if the cert is changed it will create issues for me. So I want to automate the process for the root cert.

    Regards,

    Deepak

  • Hi Deepak,

    No, the certificates needed have to be programmed on the device.

    BR,
    Gerardo