# Help with http post

**URL:** <https://forum.sierrawireless.com/t/help-with-http-post/1991>\
**Category:** Open AT\
**Created:** [March 13, 2008, 10:42pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991 "2008-03-13T22:42:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergiulungu](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sergiulungu](https://forum.sierrawireless.com/u/sergiulungu)\
**Post date:** [March 13, 2008, 10:42pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/1 "2008-03-13T22:42:59Z")

</div>

Hi  
I am trying to do to a http post put i am not succeeding in doing this. I keep getting this error WIP\_CERR\_NOT\_SUPPORTED  
I get WIP\_CEV\_OPEN then WIP\_CEV\_WRITE events and then the error occurs  
Here is my code

```auto
// HANDLER
void HTTP_hdl_c(wip_event_t *event, void *context)
{
     s32 ret;
	 s32 code;
    TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c()"));

   
    switch(event->kind)
    {
        //response header message received
        case WIP_CEV_OPEN:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_OPEN"));
        break;
       
        //response body message received
        case WIP_CEV_READ: 
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_READ"));
            //check http_status

            wip_getOpts(http_channel_pers, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
            TRACE((TRACE_LEVEL_HTTP, "return code is %d", code));
           
            if (code == 200) //OK
            {
                //... 
            }
        break;
       
        case WIP_CEV_PEER_CLOSE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_PEER_CLOSE"));
            wip_close(http_channel_pers);
        break;
       
        case WIP_CEV_ERROR:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_ERROR"));
            wip_close(http_channel_pers);
        break;
       
        //write request body
        case WIP_CEV_WRITE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_WRITE"));

            ret = wip_write(http_channel_pers, "some=request", 13);
            TRACE((TRACE_LEVEL_HTTP, "wip_write ret: %d", ret));

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

void HTTP_SendRequest()
{
	ascii* req;
    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;
    }
   
    req = "http://xxx/xxx/test.php";

    // 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);
   
}

static void gprsBearerHandler(wip_bearer_t br,s8 event,void *context){
    switch (event) {
	    case WIP_BEV_IP_CONNECTED:
			HTTP_SendRequest();
			break;
        case WIP_BEV_IP_DISCONNECTED:
            break;    
		default:        
		    break;
    }
}

// *********************************************************************************************************
// This functions will create the bearer for the GPRS
// *********************************************************************************************************
/** This functions will create the bearer for the GPRS.
@return TRUE if successfull.
@return FALSE if failure.
*/
bool createBearer( ){
    wip_bearer_t gprsBearer;
    s8 resp = 0;
    if (wip_bearerOpen(&gprsBearer,"GPRS", gprsBearerHandler, NULL) != 0 ){
        // cannot open the bearer
        return FALSE;
    }
    //configure the gprs connection
    if (wip_bearerSetOpts(gprsBearer,WIP_BOPT_GPRS_APN, Configuration.apnServer, WIP_BOPT_LOGIN,
            Configuration.gprsUser, WIP_BOPT_PASSWORD, Configuration.gprsPassword, WIP_BOPT_END) !=0){
    //cannot configure the bearer
        wip_bearerClose(gprsBearer);
         return FALSE;
    }
    // start connection
    wip_debug( "try to start bearer\n");
    resp = wip_bearerStart(gprsBearer);
    switch (resp) {
        case WIP_BERR_OK_INPROGRESS:
            wip_debug( "Bearer starting\n");
            break;
        case WIP_BERR_BAD_HDL:
            wip_debug( "Invalid bearer handle\n");
            break;
        case WIP_BERR_BAD_STATE:
            wip_debug( "WIP_BERR_BAD_STATE\n");
            break;
        case WIP_BERR_DEV:
            wip_debug( "The bearer is not stoped\n");
            break;
        default:
            wip_debug( "Unknown error when starting bearer\n");
            break;
    }
    if (resp !=WIP_BERR_OK_INPROGRESS){
        wip_bearerClose(gprsBearer);
        return FALSE;
    }
    return TRUE;
}

/ *************************************************************************** /
// Function that handles all the events related to the SIM
/ *************************************************************************** /
/** Handles all the events related to the SIM.
@param Event The events that occurs related to the SIM.
@return nothing.
*/
void ftp_simHandler(u8 Event) { 
    bool rsp;
    switch ( Event ) { 
        case ADL_SIM_EVENT_PIN_OK : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_OK" ));
            break; 
        case ADL_SIM_EVENT_REMOVED : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_REMOVED" ));
            break; 
        case ADL_SIM_EVENT_INSERTED : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_INSERTED" ));
            break; 
        case ADL_SIM_EVENT_FULL_INIT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_FULL_INIT" ));
		createBearer( );
            break; 
        case ADL_SIM_EVENT_PIN_ERROR : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_ERROR" ));
            break; 
        case ADL_SIM_EVENT_PIN_NO_ATTEMPT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_NO_ATTEMPT" ));
            break; 
        case ADL_SIM_EVENT_PIN_WAIT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_WAIT" ));
            break; 
        default : 
            TRACE (( TRACE_LEVEL_HTTP, "other PIN event" ));
            break; 
    } 

}

/ *************************************************************************** /
// initialize the SIM
/ *************************************************************************** /
void ftp_initSIM(void){
    adl_simState_e simState;
    ascii buf[50];
    simState = adl_simGetState();

    adl_atSendResponse(ADL_AT_RSP,"\n\rShould enter pin...\r\n");
    adl_simSubscribe((adl_simHdlr_f)ftp_simHandler, ftp_Configuration.pinCode);
    if (simState == ADL_SIM_STATE_PIN_WAIT){
       adl_atSendResponse(ADL_AT_RSP,"Entering pin");
      
    } else {
        adl_atSendResponse(ADL_AT_RSP,"Sim state:");
        wm_sprintf(buf,"%d",simState);
        adl_atSendResponse(ADL_AT_RSP,buf);
   }
}

/ *************************************************************************** /
// initialize the IP stack needed to put data on ftp
/ *************************************************************************** /
/** Initializes the IP stack needed to put data on ftp.
@return nothing.
*/
void initIPStack(void){
    int r;
    /* Initialize the stack */
    r = wip_netInitOpts( 
        WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1,
        WIP_NET_OPT_END); 
    if (r !=0 ){
    }
}

void init(finit_config_t * initStruct){
    Configuration = *initStruct;
    initIPStack();
    initSIM(); 
}
```

What am i doing wrong?

---

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://forum.sierrawireless.com/u/fft)\
**Post date:** [March 17, 2008, 10:28am UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/2 "2008-03-17T10:28:50Z")

</div>

- What function does return CERR\_INVALID?
- Have you got traces?

---

<div class="post-metadata">

**Author:** ![sergiulungu](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sergiulungu](https://forum.sierrawireless.com/u/sergiulungu)\
**Post date:** [March 18, 2008, 7:11pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/3 "2008-03-18T19:11:58Z")

</div>

Here is the trace  
Trace IP 20 ftp\_gprsBearerHandler event  
Trace IP 20 2  
Trace IP 20 HTTP\_SendRequest()  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_OPEN  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_WRITE  
Trace IP 20 wip\_write ret: -998  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_ERROR

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 19, 2008, 7:20am UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/4 "2008-03-19T07:20:44Z")

</div>

> [@sergiulungu](#):
>
> Trace IP 20 wip\_write ret: -998

Have you looked-up the meaning of that error code in the WIP Manual?

---

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://forum.sierrawireless.com/u/fft)\
**Post date:** [March 19, 2008, 11:13am UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/5 "2008-03-19T11:13:56Z")

</div>

in HTTP, wip\_HTTPClientCreateOpts() creates a _connection_, not a request. So you must:

- create the connection channel
- create a request channel, by performing a wip\_getFile() or wip\_putFile() on the connection
- perform read/write operations on the request.

---

<div class="post-metadata">

**Author:** ![sergiulungu](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sergiulungu](https://forum.sierrawireless.com/u/sergiulungu)\
**Post date:** [March 19, 2008, 7:41pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/6 "2008-03-19T19:41:41Z")

</div>

Ok i have managed to post some data put i still not am fully satisfied  
Here is what i do

```auto
// HANDLER
void HTTP_hdl_c(wip_event_t *event, void *context)
{
     s32 ret;
    s32 code;
    TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c()"));

    switch(event->kind)
    {
        //response header message received
        case WIP_CEV_OPEN:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_OPEN"));
        break;
       
        //response body message received
        case WIP_CEV_READ:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_READ"));
            //check http_status

            wip_getOpts(http_channel_comm, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
            TRACE((TRACE_LEVEL_HTTP, "return code is %d", code));
           
            if (code == 200) //OK
            {
                // do here the channel closing 
            }
        break;
       
        case WIP_CEV_PEER_CLOSE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_PEER_CLOSE"));
            wip_close(http_channel_comm);
        break;
       
        case WIP_CEV_ERROR:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_ERROR"));
            wip_close(http_channel_comm);
        break;
       
        //write request body
        case WIP_CEV_WRITE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_WRITE"));

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

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

void HTTP_SendRequest()
{
   ascii* req;
    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;
    }
   
    req = "http://xxx/xxx/test.php";

    // do post request
   [b]http_channel_comm [/b] = 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);
   
}

static void gprsBearerHandler(wip_bearer_t br,s8 event,void *context){
    switch (event) {
       case WIP_BEV_IP_CONNECTED:
         HTTP_SendRequest();
         break;
        case WIP_BEV_IP_DISCONNECTED:
            break;   
      default:       
          break;
    }
}

// *********************************************************************************************************
// This functions will create the bearer for the GPRS
// *********************************************************************************************************
/** This functions will create the bearer for the GPRS.
@return TRUE if successfull.
@return FALSE if failure.
*/
bool createBearer( ){
    wip_bearer_t gprsBearer;
    s8 resp = 0;
    if (wip_bearerOpen(&gprsBearer,"GPRS", gprsBearerHandler, NULL) != 0 ){
        // cannot open the bearer
        return FALSE;
    }
    //configure the gprs connection
    if (wip_bearerSetOpts(gprsBearer,WIP_BOPT_GPRS_APN, Configuration.apnServer, WIP_BOPT_LOGIN,
            Configuration.gprsUser, WIP_BOPT_PASSWORD, Configuration.gprsPassword, WIP_BOPT_END) !=0){
    //cannot configure the bearer
        wip_bearerClose(gprsBearer);
         return FALSE;
    }
    // start connection
    wip_debug( "try to start bearer\n");
    resp = wip_bearerStart(gprsBearer);
    switch (resp) {
        case WIP_BERR_OK_INPROGRESS:
            wip_debug( "Bearer starting\n");
            break;
        case WIP_BERR_BAD_HDL:
            wip_debug( "Invalid bearer handle\n");
            break;
        case WIP_BERR_BAD_STATE:
            wip_debug( "WIP_BERR_BAD_STATE\n");
            break;
        case WIP_BERR_DEV:
            wip_debug( "The bearer is not stoped\n");
            break;
        default:
            wip_debug( "Unknown error when starting bearer\n");
            break;
    }
    if (resp !=WIP_BERR_OK_INPROGRESS){
        wip_bearerClose(gprsBearer);
        return FALSE;
    }
    return TRUE;
}

/ *************************************************************************** /
// Function that handles all the events related to the SIM
/ *************************************************************************** /
/** Handles all the events related to the SIM.
@param Event The events that occurs related to the SIM.
@return nothing.
*/
void ftp_simHandler(u8 Event) {
    bool rsp;
    switch ( Event ) {
        case ADL_SIM_EVENT_PIN_OK :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_OK" ));
            break;
        case ADL_SIM_EVENT_REMOVED :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_REMOVED" ));
            break;
        case ADL_SIM_EVENT_INSERTED :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_INSERTED" ));
            break;
        case ADL_SIM_EVENT_FULL_INIT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_FULL_INIT" ));
      createBearer( );
            break;
        case ADL_SIM_EVENT_PIN_ERROR :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_ERROR" ));
            break;
        case ADL_SIM_EVENT_PIN_NO_ATTEMPT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_NO_ATTEMPT" ));
            break;
        case ADL_SIM_EVENT_PIN_WAIT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_WAIT" ));
            break;
        default :
            TRACE (( TRACE_LEVEL_HTTP, "other PIN event" ));
            break;
    }

}

/ *************************************************************************** /
// initialize the SIM
/ *************************************************************************** /
void ftp_initSIM(void){
    adl_simState_e simState;
    ascii buf[50];
    simState = adl_simGetState();

    adl_atSendResponse(ADL_AT_RSP,"\n\rShould enter pin...\r\n");
    adl_simSubscribe((adl_simHdlr_f)ftp_simHandler, ftp_Configuration.pinCode);
    if (simState == ADL_SIM_STATE_PIN_WAIT){
       adl_atSendResponse(ADL_AT_RSP,"Entering pin");
     
    } else {
        adl_atSendResponse(ADL_AT_RSP,"Sim state:");
        wm_sprintf(buf,"%d",simState);
        adl_atSendResponse(ADL_AT_RSP,buf);
   }
}

/ *************************************************************************** /
// initialize the IP stack needed to put data on ftp
/ *************************************************************************** /
/** Initializes the IP stack needed to put data on ftp.
@return nothing.
*/
void initIPStack(void){
    int r;
    /* Initialize the stack */
    r = wip_netInitOpts(
        WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1,
        WIP_NET_OPT_END);
    if (r !=0 ){
    }
}

void init(finit_config_t * initStruct){
    Configuration = *initStruct;
    initIPStack();
    initSIM();
}
```

So what i was doing wrong is that i was trying to use the connection channel instead of the request channel when i was doing the actual request.  
So thanks guys for the help.  
I still have some problems when closing the connection. After i do wip\_write i do shutdown and i get the WIP\_CEV\_READ event and the return code 200 (at this point the data appears on the site) This is the trace

Trace IP 20 HTTP\_SendRequest()  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_OPEN  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_WRITE  
Trace IP 20 wip\_write ret: 25  
Trace IP 20 wip\_shutdown ret: 0  
Trace IP 20 HTTP\_hdl\_c()  
Trace IP 20 HTTP\_hdl\_c(): WIP\_CEV\_READ  
Trace IP 20 return code is 200

At this point i would like to close all the connections if i do wip\_shutdown(http\_channel\_comm,FALSE,TRUE); again i get wip\_cerr\_cstate -999 (the channel is not in WIP\_CSTATE\_READY state) if i do wip\_close(http\_channel\_pers) i get the return code 0 (OK) but after that i get WIP\_CEV\_ERROR event  
So my question is how do i close all the channels right after completing the http post? (from what i figured i need to wait for code 200 after post so i can see the data on the site)

---

<div class="post-metadata">

**Author:** ![sergiulungu](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sergiulungu](https://forum.sierrawireless.com/u/sergiulungu)\
**Post date:** [March 19, 2008, 8:01pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/7 "2008-03-19T20:01:25Z")

</div>

I need to close all the connections because i do the posting in a loop so if i keep looping i figure that the channel created wip\_HTTPClientCreateOpts is left just hanging around and with each loop i do a new one until i get WIP\_CEV\_ERROR a and Can’t create socket: error -39

---

<div class="post-metadata">

**Author:** ![sergiulungu](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@sergiulungu](https://forum.sierrawireless.com/u/sergiulungu)\
**Post date:** [March 19, 2008, 8:24pm UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/8 "2008-03-19T20:24:14Z")

</div>

Ok i figured it out. You call wip\_shutdown when you sent all the data, this will cause to wip\_cev\_read event if the code is 200 then you can close everything like this

ret=wip\_close(http\_channel\_comm);  
TRACE((TRACE\_LEVEL\_FTP, “wip\_close ret: %d”, ret));  
ret=wip\_close(http\_channel\_pers);  
TRACE((TRACE\_LEVEL\_FTP, “wip\_close ret: %d”, ret));

---

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://forum.sierrawireless.com/u/fft)\
**Post date:** [March 20, 2008, 9:49am UTC](https://forum.sierrawireless.com/t/help-with-http-post/1991/9 "2008-03-20T09:49:25Z")

</div>

wip\_close() will do the shutdown for you if you didn’t already request it.
