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.

HTTP POST sample



Hi

I am using CCS version: 6.2.0.00050, tirtos_tivac_2_16_01_14, TivaWare_C_Series-2.1.1.71b.

There is an example for HTTP GET request in the package, was able to execute it successfully. I wanted to know if there is a sample for HTTP / HTTPS POST request. 

Thanks

Yash Bhan

  • We don't seem to have a POST example. I've asked for help with this from some of the authors of that HTTP code.

    Alan
  • We don't have a sample POST example but it would be easy to update a GET example to POST.

    You would need to do something like this in your code:

    /*
    * Note the HTTP method - HTTPStd_POST and
    * optionally set the last param to true if you have more headers to send
    */
    HTTPCli_sendRequest(&cli, HTTPStd_POST, requestURI, true);

    /* (Optional) - for sending more headers like content length (which needs to be computed for every data) */
    HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, len, true);

    /* Use this to send the POST body */
    HTTPCli_sendRequestBody(&cli, data, strlen(data));

    Rest of the code should remain the same.

    Hope this helps.

    Vikram
  • Hi Vikram

    Thank you so much for the reply.

    As per your instruction, made the changes in the code, am getting the following error:

    //********************************************************************************************************

    ss in flash

    Starting the HTTP GET example

    System provider is set to SysMin. Halt the target to view any SysMin contents in ROV.

    Service Status: DHCPC    : Enabled  :          : 000

    Service Status: DHCPC    : Enabled  : Running  : 000

    Network Added: If-1:172.28.6.153

    Service Status: DHCPC    : Enabled  : Running  : 017

    Sending a HTTP POST request to 'things.ubidots.com'

    sendRequest successful

    sendField successful

    Data sent successfully

    Error! code = 400, desc = httpTask: cannot get status

    //***********************************************************************************************************

    These are the changes in the code:

    //************************************************************************************************************
        ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);
        if (ret < 0) {
            printError("httpTask: send failed", ret);
        }
        else {
        	System_printf("sendRequest successful\n");
        }
    
        ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE, true);
    //    ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, CONTENT_LENGTH, true);
        if (ret < 0) {
        	printError("httpTask: send failed", ret);
        }
        else {
        	System_printf("sendField successful\n");
        }
    
        strcpy(data, "{\"value\":10}");
        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");
        }
    //*************************************************************************************************************************************

    Any help would be greatly appreciated.

    Thanks & Regards

    Yash Bhan

  • Yash,

    The HTTP status code 400 occurs when the request sent is malformed. I took a quick look at the your code and also the API reference for ubidots. It looks like you are trying to send a value. If that's correct, then here is the ubidots REST API example for sending a value:

    POST /api/v1.6/devices/{DATA_SOURCE_LABEL}/?token={TOKEN} HTTP/1.1
    Host: things.ubidots.com
    Content-Type: application/json
    Content-Length: 76

    {"temperature": 10, "luminosity": {"value":10}, "wind_speed": [{"value": 11, "timestamp":10000}, {"value": 12, "timestamp":13000}]}

    One obvious header that you are missing in your code is the "Content-Length" header. Without this header, the server will not know how many bytes of data to read in the HTTP body. The value of this header should be the length of data that you are sending.

    Try adding this header and also make sure other headers, labels and tokens are as per the ubidots REST API.

    Vikram
  • Hello Vikram

    Thank you so much for the help, i added the content length header and am now able to post data to the cloud, thanks a lot for the solution!
    I just wanted to know if the same is possible for the HTTPS GET sample

    Thanks
    Yash Bhan
  • Hi Yash Bhan,
    I am facing the same 400 error and not yet get solved even I worked for a week, can you please share picture how you solve it, followed same way of coding. Thank you.
    Regards,
    Raghu DS.
  • Hi Raghu,
    We generally discourage posting a new question to an old closed thread because the person who answered before may no longer be available, and also it will allow whomever is currently assigned to monitor the forum to respond to you more quickly. For these reasons, I suggest you start a new thread with your question and reference this thread.

    Thank you
  • Hello Raghu

    These are the headers that were used:

    #define HOSTNAME          "things.ubidots.com"
    #define REQUEST_URI       "/api/v1.6/devices/test/?token={value}"
    #define USER_AGENT        "HTTPCli (ARM; TI-RTOS)"
    #define CONTENT_TYPE	  "application/json"
    #define HTTPTASKSTACKSIZE 4096

    This is the httpTask function, that sends the POST request:

    Void httpTask(UArg arg0, UArg arg1)
    {
        bool moreFlag = false;
        char data[64];
        int ret;
        int len;
        char CONTENT_LENGTH[3];
        struct sockaddr_in addr;
    
    
        //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_Struct cli;
        HTTPCli_Field fields[3] = {
            { HTTPStd_FIELD_NAME_HOST, HOSTNAME },
            { HTTPStd_FIELD_NAME_USER_AGENT, USER_AGENT },
            { NULL, NULL }
        };
    
        System_printf("Sending a HTTP POST 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) {
            printError("httpTask: address resolution failed", ret);
        }
    
        ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, NULL);
        if (ret < 0) {
            printError("httpTask: connect failed", ret);
        }
    
    //************************************************************************************************************
        ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);
        if (ret < 0) {
            printError("httpTask: 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("httpTask: 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("httpTask: response field processing failed", ret);
        }
    
        len = 0;
        do {
            ret = HTTPCli_readResponseBody(&cli, data, sizeof(data), &moreFlag);
            if (ret < 0) {
                printError("httpTask: response body processing failed", ret);
            }
    
            len += ret;
        } while (moreFlag);
    
        System_printf("Received %d bytes of pay-load\n", len);
        System_flush();
    
        HTTPCli_disconnect(&cli);
        HTTPCli_destruct(&cli);
    }

  • Hi Yash Bhan,
    Thanks for the reply, I done with this.
    And you are trying for the "HttpsPost", are you done with this.
    I am also trying the HttpsPost and I was getting the Gateway Timeout respose code "504".

    regards,
    Raghu DS
  • Hi Raghu

    I'm trying for HTTPS POST, but am also getting the same error

    Regards
    Yash Bhan
  • Hi yash Bhan,
    It seems, this is server problem please check "www.keycdn.com/.../"
    Are you tried with any of your server?
    We are till creating the "https" server, afterwards has to check.
    Please keep on updating I will also do.

    Regards,
    Raghu DS
  • Hello Raghu

    Im trying to connect to AWS server, they use only HTTPS.
    Am facing issues with the same.

    Regards
    Yash Bhan
  • Hi Yash,

    The error 504 is resolved for me. This is because the defined content length sending should be equal to the buffer length. As my content length is more than the buffer length the server is waiting for remaining buffer, so time out happening. The content length can be less than the buffer but not more than, please make sure this.

    Regards,

    Raghu DS.