HTTP post data format
Antwort
10.04.19 09:40

Hi everyone,

 Recently I tried to use HTTP API to post data from the XDK sensor to Thingsboard by modifying the example project HttpExampleClient.

Here is part of the code

In static void AppControllerFire(void* pvParameters

{

        Sensor_Value_T sensorValue;

        char request_body[256];
        memset(&sensorValue, 0x00, sizeof(sensorValue));

   ...

 while (1)

    {

    snprintf((char *) request_body, APP_HTTP_DATA_BUFFER_SIZE,
    "{ \"Humidity\": \"%ld\", \"Pressure\": \"%ld\", \"Temperature\": \"%f\" }",
    (long int) sensorValue.RH, (long int) sensorValue.Pressure,
     (sensorValue.Temp /= 1000));

    printf("%s\n",HTTPRestClientPostInfo.Payload);
    HTTPRestClientPostInfo.PayloadLength = (sizeof(HTTPRestClientPostInfo.Payload));
     retcode = HTTPRestClient_Post(&HTTPRestClientConfigInfo, &HTTPRestClientPostInfo, APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT);

    }

}

...

Wifi connection and is successful and the POST request is successfully sent. But the platform cannot receive the data and the console output is

INFO | XDK DEVICE 1: Connected to WPA network successfully
...
 INFO | XDK DEVICE 1: WLANPersonalWPA2Connect : Connected to WPA network successfully. 
 INFO | XDK DEVICE 1: { "Humidity": "38", "Temperature": "27.000000" }
 INFO | XDK DEVICE 1: HttpRestClientRequestSentCallback: HTTP request sent successfully.
 INFO | XDK DEVICE 1: HttpRestClientPostResponseCallback: received HTTP status other than 200 OK. status=500
 INFO | XDK DEVICE 1: HttpRestClientPostResponseCallback: HTTP rest client POST was successful.

 

If I directly use the request body

#define POST_REQUEST_BODY               "{ \"device\": \"XDK110\", \"ping\": \"pong\" }"

It can be received. So I think maybe the problem lies in the data format I am using. I have tried to define the message without " " around the data. The error is the same.

I am confused, can anyone help me.

Thank you in advance.

 

 

0 (0 Stimmen)
RE: HTTP post data format
Antwort
11.04.19 09:29 als Antwort auf Sisi Miao.

Hi Sisi Miao,

I tried your payload format as below in 2 ways and post is successful.

/* payload option 1 */
POST_REQUEST_BODY               "{ \"Humidity\": \"38\", \"Temperature\": \"27.000000\" }"
/* payload option 2 */
snprintf((char *) HTTPRestClientPostInfo.Payload, 256,"{ \"Humidity\": \"%ld\", \"Pressure\": \"%ld\", \"Temperature\": \"%f\" }",10, 20, 30);
HTTPRestClientPostInfo.PayloadLength = (sizeof(HTTPRestClientPostInfo.Payload));

I find your code and console message contradictory from your post! In your code you have added temp, pressure, humidity to the payload; but console message does not print "Pressure".

0 (0 Stimmen)
RE: HTTP post data format
Antwort
16.04.19 13:43 als Antwort auf Padmapriya G.

Hi Padmapriya G,
sorry for the inconsistency, I've changed the code too many times so it got a bit messy.
And thank you for your time, I've tried your two options, the first one works but the second one does not. But I've found the problem with my code. It is actually because of the payload length. If I use request_body =  "{ \"Humidity\": \"30\", \"Pressure\": \"96403\", \"Temperature\": \"23.261000\" }", and then use HTTPRestClientPostInfo.PayloadLength = (sizeof(request_body)); The payload length will be set as 8(or something else),but in fact it should be 70-1=69. So the send is unsuccessful. That's why your first option works because if we use #define, the length can be correctly determined.
The reason that we successfully post the data could be that the HTTPRestClientPostInfo is static so the snprintf didn't change it.
I've changed my code as follows:
...
if (RETCODE_OK == retcode)
        {

            snprintf((char *) request_body, APP_HTTP_DATA_BUFFER_SIZE,
                    "{ \"Humidity\": \"%ld\", \"Pressure\": \"%ld\", \"Temperature\": \"%f\" }",
                    (long int) sensorValue.RH, (long int) sensorValue.Pressure,
                                (sensorValue.Temp /= 1000));

            HTTPRestClient_Post_T HTTPRestClientPostInfo =
                    {
                            .Payload =  request_body ,
                            .PayloadLength = 69,
                            .Url = DEST_POST_PATH,
                            .RequestCustomHeader0 = POST_REQUEST_CUSTOM_HEADER_0,
                            .RequestCustomHeader1 = POST_REQUEST_CUSTOM_HEADER_1,
                    }; /**< HTTP rest client POST parameters */


            retcode = HTTPRestClient_Post(&HTTPRestClientConfigInfo, &HTTPRestClientPostInfo, APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT);
        }
...

 

+2 (2 Stimmen)