How to handle HTTP POST Response?

Hi all,

First of all, I must say that I am newbie on Open AT and embedded modules. Please accept my apologies if I ask stupid questions. :slight_smile:

I am using Q2687H embedded modules and developing an application to connect remote server, sending/receiving data with HTTP Client. I have some troubles with handling HTTP POST Response. I’ve used the sample codes provided by Developer Studio. It worked but when I send an HTTP POST request to server, I only can see the results on my TMConsole like following;

TMConsole logs:

HTTP Client Service test application : Init

GPRS: CONNECTED IP=92.64.193.57 DNS1=212.65.140.135 DNS2=212.65.156.200
http_ClientDataHandler: Start

http_ClientDataHandler: WIP_CEV_WRITE

http_ClientDataHandler: 73 bytes written
{"access_token":"3EMs2OvAhA6e9wzw1SPMpMArvQ08uQQM1-O3NLVsmmT7VLqeOvMzW1XBeQMxxUP8IiKN_whh63D-OFT5gifeRIyqB22Dr2wbJnQTvoDw2kWq1PMiRF1K1rw7IqiPya1DltFKvSwyqHMNojfa2f25Ju5daUOBNnhNA1xGC0RN4_W6P5-BHfxwXSGvsQoJzZLMhx3GAKop24K29_h1M7qi6icTuctKEFqGWI0EGmsFf_3wFzcpO2yCithHHmDCQKPAHp7Gf4J1K0gEVbzXMXkqaO087Tw5_FYZBCnaHs7d6TZttOCaQiNMuSMCUdLluv7-b_AvoFLmQtQuK_iocmOqGGiiM82ieWg9UREp-zUO7l22sPm68Cm6u3R6KmWdeQ3WCy5H6hQsbiOYo8yBMEGsSkp5BUtcXV20MNkrwa5_Wka_SRc_McmqK5eA67K4vuadSnFQ17qbzAHAjXoJt1YNtg","token_type":"bearer","expires_in":7199,"refresh_token":"cONjjU_LHm3AZVVxf048LaIdQ8dhu-0Ytny5dkrrA3M2M_ivBGypm4Cxi1RZQerUJSu5Bq6HV-Ay_iwu0dUDfFq4BOVaDgdL7J_kFme1mARJAB3flm77F25W2bn8UtFvOem31HhSWDCwyp_E5V652MNQRBdvZlrOouhYAIdo5CIIHLuxPepcfmKc9nFEl7EatRA56obaLTMEW6UuTGJjpcLxOkzB6M9QLGAWUghn_LZ3A4OB1Wqi-1HwWAvZS-dxv37gwawh_YjPJJHBA6xUqakLZ2uAN3zIK4ZWVOT0ZJA6sbP6H7UFQSvcBeDH3CfNBk8u01sh_xOHOM_yy9ElO2ZbvyyhXhhXji4F4OTHrXblMXLc75KXZmZXo_aZn5y0eI7ji7BztsyvHIDCmv3Kr3E9U_dm8CCik1mbXPkrPDSgruS9aC97t6DfBMjKzUzUf2nfJ5wv2xImSQR_KVlH-Q"}
http_ClientDataHandler: Done
http_ClientDataHandler: Status=200
http_ClientDataHandler: Reason="OK"

I need to handle the "{“access_token”:"3EMs2OvAhA6e9wzw1SPM… " part of the response data but I could not find the way how to handle it in WIP_CEV_READ or WIP_CEV_WRITE in event handler :frowning:

Edit: Here is my handler code;

ascii *postData = "grant_type=password&UserName=usrName&Password=asdfghjkl#$123&CityCode=46";

static void http_ClientDataHandler ( wip_event_t *ev, void *ctx )
{
    ascii Ptr_OnTrace [ 240 ];
    int len, tmplen;
    s32 status;
    http_ClientCtx_t *pHttpClientCtx = ( http_ClientCtx_t * ) &http_ClientCtx;	

    switch ( ev->kind )
    {
        case WIP_CEV_OPEN:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: WIP_CEV_OPEN" ) );
            adl_atSendResponse ( ADL_AT_UNS, "http_ClientDataHandler: Start\r\n" );
            /* ready for getting response data  */
            pHttpClientCtx->dataLength = 0;
            break;
        }

        case WIP_CEV_READ:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: WIP_CEV_READ" ) );
            /* we must read all available data to trigger WIP_CEV_READ again    */
            tmplen = 0;
            while ( ( len = wip_read ( ev->channel, tmpbuf, sizeof ( tmpbuf ) - 1 ) ) > 0 )
            {
                tmpbuf [ len ] = 0;
                adl_atSendResponse ( ADL_AT_UNS, tmpbuf );
                tmplen += len;
            }
            TRACE(( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: read %d bytes", tmplen ));
            /* compute total length of response */
            pHttpClientCtx->dataLength += tmplen;

            break;
        }
        case WIP_CEV_WRITE:
        {
        		TRACE(( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: WIP_CEV_WRITE\n" ));
        		adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientDataHandler: WIP_CEV_WRITE\r\n");

        		sentBytes = wip_write( pHttpClientCtx->DataChannel, postData, wm_strlen(postData));

        		if(sentBytes < 0)
        		{/* error */
        			adl_atSendResponse ( ADL_AT_UNS, "\r\nwip_write error\r\n" );
        			return;
        		}
        		else
        		{
        			TRACE(( 4, "http_ClientDataHandler: bytes %d written\n", sentBytes ));
        			wm_sprintf( tmpbuf, "\r\nhttp_ClientDataHandler: %d bytes written\r\n", sentBytes);
        			adl_atSendResponse ( ADL_AT_UNS, tmpbuf);
        		}
        		wip_shutdown( pHttpClientCtx->DataChannel, FALSE, TRUE);
        		break;
        }
        case WIP_CEV_PEER_CLOSE:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: WIP_CEV_PEER_CLOSE" ) );
            adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientDataHandler: Done\r\n" );

            /* end of data  */
            /* show response information    */
            if ( wip_getOpts ( ev->channel,
                               WIP_COPT_HTTP_STATUS_CODE, &status,
                               WIP_COPT_HTTP_STATUS_REASON, tmpbuf, sizeof ( tmpbuf ),
                               WIP_COPT_END ) == OK )
            {
                ascii sbuf [ 16 ];
                adl_atSendResponse ( ADL_AT_UNS, "http_ClientDataHandler: Status=" );
                wm_sprintf ( sbuf, "%d", ( s16 ) status );
                adl_atSendResponse ( ADL_AT_UNS, sbuf );
                adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientDataHandler: Reason=\"" );
                adl_atSendResponse ( ADL_AT_UNS, tmpbuf );
                adl_atSendResponse ( ADL_AT_UNS, "\"\r\n" );
                TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: Status=%d", status ) );
                wm_sprintf ( Ptr_OnTrace, "http_ClientDataHandler: Reason=\"%s\"", tmpbuf );
                TRACE ( ( NORMAL_TRACE_LEVEL, Ptr_OnTrace ) );
            }

            if ( wip_getOpts ( ev->channel,
                               WIP_COPT_HTTP_HEADER, "content-type", tmpbuf, sizeof ( tmpbuf ),
                               WIP_COPT_END ) == OK )
            {
                wm_sprintf ( Ptr_OnTrace, "http_ClientDataHandler: Content Type=\"%s\"\n", tmpbuf );
                TRACE ( ( NORMAL_TRACE_LEVEL, Ptr_OnTrace ) );
            }
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientDataHandler: Response Length=%d bytes", pHttpClientCtx->dataLength ) );

            /* data channel must be closed*/
            wip_close( ev->channel );
            break;
        }

        case WIP_CEV_ERROR:
        {
            TRACE ( ( ERROR_TRACE_LEVEL, "http_ClientDataHandler: WIP_CEV_ERROR %d", ev->content.error.errnum ) );
            adl_atSendResponse ( ADL_AT_UNS, "http_ClientDataHandler: ERROR\r\n" );
            /* connection to server broken  */
            wip_close( ev->channel);
            break;
        }

        default:
        {
            TRACE ( ( ERROR_TRACE_LEVEL, "http_ClientDataHandler: unexpected event: %d", ev->kind ) );
            break;
        }
    }
}

Can you tell me if you have any idea?

Thanks in advance.

Best regards

MÇ.

Hi all,

I finally found the way to handle incoming data. I am so sorry to bother you for such simple question :sweat:

For newbies like me if you have such problems, the answer is tmpbuf variable in while in WIP_CEV_READ event.

Best regards

MÇ.

I still have problems handling the HTTP POST response. I tried to find the answer here, but so far unsuccessfully. Could you take a screenshot or explain in more detail?

Hi @daveco,

Can you see the messages on your Trace windows like “http_ClientDataHandler: read xx bytes” ? If so, you can see the data in tmpbuf variable in “case WIP_CEV_READ:”.

Regards.

I still experience http post responses. Though I am not a developer, but I tried my self to fix the issue. I have tried using this url to know some thing. I faced this type of problem for my website penmypaper. May the article link can help you.

I think the best way to deal with such situations is to consult with professionals at eduhelphub , their tutorials, courses and guidance can help you out with any coding lessons. you can even talk to them directly if you are having any issues with your project.