Forums - Issue In HTTP Posting Request On MDM9206

2 posts / 0 new
Last post
Issue In HTTP Posting Request On MDM9206
naman.shandilya
Join Date: 27 Jul 21
Posts: 1
Posted: Wed, 2021-07-28 23:53

Hi,

I am working on HTTP Post request on MDM9206. My device is connectin gto the server successfully but when i post data to the server it returns code 500. I am unable to understand the post header format and post body format. Snippet of the code is as follows

server url "http://dev.salvator.com.au:3000/device/sos";

void http_post_body(char *TxBuffer)
{
    // char TxBuffer[240] = {0};
    uint16_t lenpoststatus;
    char DeviceId[20] = {0};
    uint32_t in_sosType = 1;
    static char sos_lat[16]= {0}, sos_long[16]= {0};
    static bool CellIdLocFlag = 1;

        strncpy(TxBuffer"{\"PacketType\":\"Alert\",\"Payload\":"sizeof("{\"PacketType\":\"Alert\",\"Payload\":"));

        strncat(TxBuffer"{\"TriggeredBy\":"sizeof("{\"TriggeredBy\":"));
        len = strlen(TxBuffer);

        TxBuffer[len]=48+in_sosType;
        TxBuffer[len+1]='\0';

        strncat(TxBuffer","sizeof(","));
        strncat(TxBuffer"\"DeviceId\":\""sizeof("\"DeviceId\":\""));
        strncat(TxBufferDeviceIdsizeof(DeviceId));
        strncat(TxBuffer"\","sizeof("\","));

        strncat(TxBuffer"\"Lat\":"sizeof("\"Lat\":"));
        strncat(TxBuffersos_latsizeof(sos_lat));
        strncat(TxBuffer",\"Long\":"sizeof(",\"Long\":"));
        strncat(TxBuffersos_longsizeof(sos_long));

        strncat(TxBuffer",\"LocStatus\":"sizeof("\"LocStatus\":"));
        uint16_t l1 = strlen(TxBuffer);
        itoa(CellIdLocFlag, &TxBuffer[l1], 10);

        strncat(TxBuffer",\"Status\":\"Active\""sizeof(",\"Status\":\"Active\""));
        strncat(TxBuffer"}}"sizeof("}}")); 
 
    SALVATOR_UART_DBG("\r\n%d\r\n", (uint32_t)strlen(TxBuffer));
    // SALVATOR_UART_DBG("\r\n%d", sizeof(TxBuffer));
    SALVATOR_UART_DBG("\r\n%s\r\n"TxBuffer);    

    qapi_Net_HTTPc_Set_Body(http_handle, (const char *)TxBuffer, (uint32_t)strlen(TxBuffer));
}
 
 
int http_post_data(charhostuint16 portuint8file_name)
{
    qapi_Status_t ret = -1;
    uint8 reconnect_count = 0;
#ifdef QUECTEL_HTTPS_SUPPORT
    SSL_INST http_ssl;
#endif

    /* Start HTTPc service */
    qapi_Net_HTTPc_Start();
    memset(&http_session_policy0sizeof(http_session_policy_t));
#ifdef QUECTEL_HTTPS_SUPPORT
    if (ssl_ctx_id)
    {
        /* config object and certificate */
        if (0 != http_ssl_config(&http_ssl))
        {
            SALVATOR_UART_DBG("ERROR: Config ssl object and certificates error");
            
        }
        
        /* create a SSL session */
        ssl_obj_hdl = http_ssl.sslCtx;
        http_ssl.sslCon = qapi_Net_SSL_Con_New(http_ssl.sslCtx, QAPI_NET_SSL_TLS_E);
        if (http_ssl.sslCon == QAPI_NET_SSL_INVALID_HANDLE)
        {
            SALVATOR_UART_DBG("ERROR: Unable to create SSL context");
            return -1;
        }
        
        if (QAPI_OK != qapi_Net_SSL_Configure(http_ssl.sslCon, &http_ssl.config))
        {
            SALVATOR_UART_DBG("ERROR: SSL configure failed");
            return -2;
        }

        http_handle = qapi_Net_HTTPc_New_sess(20000http_ssl.sslCtx, http_client_cb, NULL10241024);
        ret = qapi_Net_HTTPc_Configure_SSL(http_handle, &http_ssl.config);
        if(ret != QAPI_OK)
        {
            SALVATOR_UART_DBG("Start HTTPS connection, SSL config ret[%d]", ret);
            return -2;
        }

    }
    else
    {
        http_handle = qapi_Net_HTTPc_New_sess(200000, http_client_cb, NULL10241024);

    }
#else
    http_handle = qapi_Net_HTTPc_New_sess(200000http_client_cbNULL5 * 10245 * 1024);
#endif /* QUECTEL_HTTPS_SUPPORT */

    http_session_policy.session_state = HTTP_SESSION_INIT;

    if ( http_handle == NULL)
    {
        SALVATOR_UART_DBG("qapi_Net_HTTPc_New_sess ERROR");
        return -3;
    }

    // qapi_Net_HTTPc_Pass_Pool_Ptr(http_handle, byte_pool_salvator);
    
    do
    {
        ret = qapi_Net_HTTPc_Connect(http_handlehostport);
        if (ret != QAPI_OK)
        {
            reconnect_count++;
            SALVATOR_UART_DBG("qapi_Net_HTTPc_Connect ERROR ret:%d, re-connect times:%d",ret,reconnect_count);
            if(reconnect_count >= HTPP_MAX_CONNECT_RETRY)
            {
                return -4;
            }
            qapi_Timer_Sleep(3QAPI_TIMER_UNIT_SECtrue);
        }
        else
        {
            SALVATOR_UART_DBG("qapi_Net_HTTPc_Connect success :%d"ret);
            break;
        }
    } while (1);
    http_session_policy.session_state = HTTP_SESSION_CONN;

    qapi_Net_HTTPc_Clear_Header(http_handle);

    qapi_Net_HTTPc_Add_Header_Field(http_handle, "Connection", "Keep-Alive");

    qapi_Net_HTTPc_Add_Header_Field(http_handle, "Content-Type", "application/x-www-form-urlencoded");

    qapi_Net_HTTPc_Add_Header_Field(http_handle, "Content-Length", strlen(Tx_Buffer)); // Not sure if required 

    http_post_body((char *)Tx_Buffer);

    /* get response content */
    ret = qapi_Net_HTTPc_Request(http_handleQAPI_NET_HTTP_CLIENT_POST_Efile_name); 
    if (ret != QAPI_OK)
    {
        SALVATOR_UART_DBG("qapi_Net_HTTPc_Request ERROR :%d",ret);
        return -5;
    }
    // http_post_body(Tx_Buffer);
    return 0;
}

 

void http_client_cb(voidargint32 statevoidhttp_resp)
{
    boolean ret_val = FALSE;
    qapi_Net_HTTPc_Response_t * resp = NULL;
    resp = (qapi_Net_HTTPc_Response_t *)http_resp;
    http_session_policy.session_state = HTTP_SESSION_SOS_RESPONSE;
    // SALVATOR_UART_DBG("\r\nhttp_client_cb:%x,%ld,len:%lu,code:%lu,data%s,resp_header%s\r\n",arg,state,resp->length,resp->resp_Code,resp->data,resp->rsp_hdr);
    
    SALVATOR_UART_DBG("\r\ncode:%lu,data%s\r\n"resp->resp_Code,resp->data);
}

 

  • Up0
  • Down0
ss.pandiri
Join Date: 29 May 18
Posts: 48
Posted: Mon, 2021-08-02 08:30

hi,

Just went through the code, it looks ok. HTTP 500 error should be a server side issue. Can you verify with any other POST API with simple content?

Is there any documentation available for /sos POST REST API?

thanks,

Steve 

  • Up0
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.