Sending HTTP POST


#1

Hi!

I am trying to send a HTTP Post message.
As documented, I am using the HTTP Client. Consider the following code:

void HTTP_SendRequest()
{
    TRACE((TL_SOCK, "HTTP_SendRequest()"));
    http_channel_pers = wip_HTTPClientCreateOpts(NULL, NULL, WIP_COPT_HTTP_HEADER, "User-Agent", "WIP-HTTP-Client/1.0", WIP_COPT_END);
    if (http_channel_pers == NULL)
    {
        return;
    }
    HTTP_SwitchState(HTTPSENDREQUEST);
    
    char* req = "http://195.158.145.72/demo/Connector/StandardConnector.aspx?serial=XXX&auth=XXX";

    // do post request
    wip_getFileOpts(http_channel_pers, req, HTTP_hdl_c, NULL,
         WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
         WIP_COPT_HTTP_HEADER, "Accept", "*",
         WIP_COPT_END);
    
}


// HANDLER
void HTTP_hdl_c(wip_event_t *event, void *context)
{
  
    TRACE((TL_SOCK, "HTTP_hdl_c()"));
    
    http_channel_comm = event->channel;
    s32 ret;
    
    switch(event->kind)
    {
        //response header message received
        case WIP_CEV_OPEN:
            TRACE((TL_SOCK, "HTTP_hdl_c(): WIP_CEV_OPEN"));
            //as per documentation, in CEV_OPEN the HTTP_STATUS_CODE should be recieved.
            //anyway this only works after receiving a CEV_READ event
        break;
        
        //response body message received
        case WIP_CEV_READ:  
            TRACE((TL_SOCK, "HTTP_hdl_c(): WIP_CEV_READ"));
            //check http_status
            s32 code;
            wip_getOpts(http_channel_comm, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
            TRACE((TL_SOCK, "return code is %d", code));
            
            if (code == 200) //OK
            {
                //...  
            }
        break;
        
        case WIP_CEV_PEER_CLOSE:
            TRACE((TL_SOCK, "HTTP_hdl_c(): WIP_CEV_PEER_CLOSE"));
            wip_close(http_channel_comm);
            HTTP_SwitchState(HTTPREQUESTDONE);
        break;
        
        case WIP_CEV_ERROR:
            TRACE((TL_SOCK, "HTTP_hdl_c(): WIP_CEV_ERROR"));
            wip_close(http_channel_comm);
            HTTP_SwitchState(HTTPREQUESTERROR);
        break;
        
        //write request body
        case WIP_CEV_WRITE:
            TRACE((TL_SOCK, "HTTP_hdl_c(): WIP_CEV_WRITE"));

            ret = wip_write(http_channel_comm, "some request", 12);
            TRACE((TL_SOCK, "wip_write ret: %d", ret));

            wip_shutdown(http_channel_comm,FALSE,TRUE); 
        break;
    }
}

My problem is, that after sending some request body after the CEV_WRITE Event in the HTTP Handler, an exception occurs:

Trace CUS4 2 HTTP_SendRequest()
Trace CUS4 2 HTTP_hdl_c()
Trace CUS4 2 HTTP_hdl_c(): WIP_CEV_OPEN
Trace CUS4 2 HTTP_hdl_c()
Trace CUS4 2 HTTP_hdl_c(): WIP_CEV_WRITE
Trace CUS4 2 wip_write ret: 12
ADS Bckt -> Unresolved symbol : 0x00053100
ADS Bckt -> Unresolved symbol : 0x00050CB7
ADS Bckt -------> ADS BACK TRACE <-------
ADS Bckt The function corresponding to this address : 0xbe, is not in the .axf file

Anybody know, what I am doing wrong? Trying to wip_shutdown the channel after the wip_write has the same effect.


#2

Tried calling wip_shutdown through a timer instead?


#3

But the code you posted shows the wip_shutdown after the wip_write anyhow?
So what was the alternative?


#4

There are, indeed, many cases where Open-AT seems to have timing issues - but I don’t think this is one of them, as I call wip_shutdown “immediately” after wip_write - and it works OK for me! 8)

What is the return value from wip_shutdown?

Are you sure you’re using the correct channel?


#5

Hi awneil,

thanks for your reply. I am using the channel

event->channel;

in the handler. Shall I use the other channel, which is created with

wip_getFileOpts(http_channel_pers, req, HTTP_hdl_c, NULL,
         WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
         WIP_COPT_HTTP_HEADER, "Accept", "*",
         WIP_COPT_END);

The way wip_shutdown is called here, shuts down the socket for writing. Why do I have to call it anyway?

The return value of wip_shutdown is 0.


#6

I am now using the latest version of WIP (3.0.0.2.1).

The example above is now working fine! :smiley: