# WIP HTTP POST problems

**URL:** https://forum.sierrawireless.com/t/wip-http-post-problems/4220
**Category:** Open AT
**Created:** [February 22, 2010, 12:23pm UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220 "2010-02-22T12:23:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![peter\_mcc](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@peter\_mcc](https://forum.sierrawireless.com/u/peter_mcc)
#### Post date: [February 22, 2010, 12:23pm UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/1 "2010-02-22T12:23:26Z")

</div>

hi  
I’m trying to make a system to send data to our website. It mostly works but isn’t very reliable. The system is running on a Fastrax Supreme 10 modem at the moment, running inside the modem.

It has a few tasks doing various tasks -

- the main task initialises the WIP stack
- one task manages the comunications on UART2
- one task processes the messages on UART2 (ie manages the comms protocol)
- one task looks after sending data over the GPRS link

It all goes well for a while - I can make somewhere between 5 & 50 HTTP POST requests before it stops working. So far I can’t work out how to recover.

For each request I do the following (simplified)

- create a HTTP Session Channel
- create a HTTP Data channel
- Wait for WIP\_CEV\_OPEN event (which I ignore)
- Wait for WIP\_CEV\_WRITE event
- send the POST data
- if I get any WIP\_CEV\_READ events I read all the data
- Wait for WIP\_CEV\_PEER\_CLOSE event
- Close the session channel
- close the data channel
- wait for the next message to arrive

When it fails I never get the WIP\_CEV\_OPEN or WIP\_CEV\_WRITE events. My code times out after 20 seconds but I’m not sure how to recover.

I’m using M2M Studio 1.02, my modem has firmwar R74\_00gg.FSU0004 and my application OS is 6.30.

---

<div class="post-metadata">

### Author: ![charlvz](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@charlvz](https://forum.sierrawireless.com/u/charlvz)
#### Post date: [February 23, 2010, 10:42am UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/2 "2010-02-23T10:42:43Z")

</div>

> [@peter\_mcc](#):
>
> Wait for WIP\_CEV\_PEER\_CLOSE event

My suggestion is to use a timer to make sure the WIP\_CEV\_PEER\_CLOSE event is received, and if it is not received after 10seconds or so manually close it. If the connection to the server is never closed you would not be able to re-open it.

While connected to the server if any events are not received your process will fail, so to make the system robust use timers to make sure that events are received. If they aren’t, close the connection and re-open and retry.

Hope it helps

---

<div class="post-metadata">

### Author: ![gava100](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@gava100](https://forum.sierrawireless.com/u/gava100)
#### Post date: [June 18, 2010, 12:31pm UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/3 "2010-06-18T12:31:44Z")

</div>

Hello Peter,

Please, would you mind sending me a piece of code showing how you are sending data via HTTP POST to the server? I’m trying to use wip\_write but it’s adding 2 additional bytes in the payload as the buffer size on the payload. See bellow and note the 5f (95 decimal) at the beginning the the content.

POST /HelloWMP/HelloServlet HTTP/1.1  
Host: myserver.com.br:8088  
Transfer-Encoding: chunked  
User-Agent: WIPHTTP/1.0  
Content-Type: application/x-www-form-urlencoded  
Content-Length: 95

5f  
p1=This is a big message that I want to send using the wip\_write command via POST http method!

Regards

Rafael

---

<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: [June 18, 2010, 12:45pm UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/4 "2010-06-18T12:45:19Z")

</div>

It is working perfectly correctly & normally - that’s what “chunked” encoding means! 8)

---

<div class="post-metadata">

### Author: ![Vamsi457](https://avatars.discourse-cdn.com/v4/letter/v/bbce88/32.png) [@Vamsi457](https://forum.sierrawireless.com/u/Vamsi457)
#### Post date: [January 22, 2011, 11:01am UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/5 "2011-01-22T11:01:35Z")

</div>

Hi,  
I need to send data to web server using HTTP Post method. I tried the code given in these forums. I was able to send data through HTTP post method only once. For second time I am getting HTTP 400 Error Response. I have to restart my Wavecom Modem in-order to send data again. Please suggest me any solution regarding this issue. In this post its said to close channels manully, but while doing so it says I am trying to close channel which is null. My code is as follows:

```auto
#include	"wip.h"

#define RECBUFF 400

/////////External Function Declarations
extern void InboundData(char *Data);
/////////Local Function Declarations
s32 http_Client(void);
void KillHTTP(void);

////////External Variable Declarations
extern char *Data_PCKT;
////////Local Variable Declarations
static u8 rcv_buffer [RECBUFF];
const ascii * HTTP_STR_URL = "http://lightmatrix.net/TEST/DEFAULT.ASPX";
int BytesWritten, code;
s32 status;
ascii info[50];
ascii *tmpbuf;
static int offset = 0;

typedef struct
{
	wip_channel_t CnxChannel; // session channel
	wip_channel_t DataChannel; // data channel
	u32 dataLength; // response data length

}http_ClientTestCtx_t;

http_ClientTestCtx_t http_ClientTestCtx;

void KillHTTP(void)
{
	int i = 0;
	wip_debug("\r\nKilling HTTP Handler...\r\n");
	if (http_ClientTestCtx.CnxChannel != NULL)
		http_ClientTestCtx.CnxChannel = NULL;
	if (http_ClientTestCtx.DataChannel != NULL)
		http_ClientTestCtx.DataChannel = NULL;

	for(i=0;i<RECBUFF;i++)
	{
		rcv_buffer[i] = 0;
	}
	for(i=0;i<50;i++)
	{
		info[i] = 0;
	}
	wip_debug("\r\nHTTP Handler killed...\r\n");
}

static void http_ClientTestDataHandler( wip_event_t *ev, void *ctx)
{

	//s16 m,n,p,w;
	int tmp = 0;
		http_ClientTestCtx_t *pHttpClientCtx = (http_ClientTestCtx_t *) &http_ClientTestCtx;
	switch(ev->kind)
	{
		case WIP_CEV_OPEN: wip_debug("\r\nhttp_ClientTestDataHandler: Start\r\n");

												break;

		case WIP_CEV_READ: wip_debug("\r\nhttp_ClientTestDataHandler: WIP_CEV_READ\r\n");
												wip_getOpts(http_ClientTestCtx.DataChannel, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
												wm_sprintf( info, "\r\nHTTP Status code: %d\r\n", code);
												wip_debug(info);
												if (code == 200)
												{
													int nread,i;
													nread = wip_read( ev->channel, rcv_buffer,RECBUFF);
													 if( nread < 0)
													 {
														  wip_debug( "\n[client]Unable to read. Error %i\n", nread);
														  return;
													 }
													 // wip_debug( "\n[client]Data received is %s\n", rcv_buffer);
													 nread = -1;
													 tmp = 0;
												 	 for(i=0;i<RECBUFF;i++)
													 {
														 if(rcv_buffer[i] == '~')
														 {
															 tmp = 1;
														 }
														 if(tmp==1)
														 {
															 rcv_buffer[i] = '\0';
														 }

													 }
												 	InboundData(rcv_buffer);

												 	do
												 	{

												 		wip_debug( "\n[client]Trying to close Data Channel.\n");
												 		wip_close( http_ClientTestCtx.DataChannel);
												 		adl_ctxSleep(100);

												 	}while(wip_getState(http_ClientTestCtx.DataChannel) == WIP_CSTATE_READY);
												 	do
												 	{
												 		wip_debug( "\n[client]Trying to close CX Channel.\n");
												 		wip_close( http_ClientTestCtx.CnxChannel);
												 		adl_ctxSleep(100);
												 	}while(wip_getState(http_ClientTestCtx.CnxChannel) == WIP_CSTATE_READY);
												 	adl_ctxSleep(2000);
												 	Business_Logic();

												}
												if (code == 400)
												{
													wip_debug( "\nChannel Unstable...\n");
													wip_debug( "\n[client]Trying to close CX Channel.\n");
													wip_close( http_ClientTestCtx.CnxChannel);
													if(wip_getState(http_ClientTestCtx.CnxChannel) == WIP_CSTATE_READY)
														wip_debug( "\n[client]Successfully to close CX Channel.\n");
													else
														wip_debug( "\n[client]Failed to close CX Channel.\n");

												}
												code = 0;
											//	wip_close( http_ClientTestCtx.DataChannel);
											//	wip_close( http_ClientTestCtx.CnxChannel);
												break;

		case WIP_CEV_WRITE:
												tmpbuf = 0;
												tmpbuf = adl_memGet(strlen(Data_PCKT)+3);
												wm_sprintf(tmpbuf,"Q=%s",Data_PCKT);
												wip_debug("\nSize of TMPBUF is %d. TMPBUF has : %s\n",strlen(tmpbuf),tmpbuf);

												wip_debug("\r\nhttp_ClientTestDataHandler: WIP_CEV_WRITE\r\n");
												BytesWritten = wip_write( http_ClientTestCtx.DataChannel, tmpbuf, (strlen(tmpbuf)+1)-offset);
												if(BytesWritten < 0)
												{/* error */
													wip_debug( "HTTP write error %i\n", BytesWritten);
													wip_debug( "\r\nwip_write error\r\n" );
													return;
												}
												offset += BytesWritten; /* Update the number of bytes sent. */
												if( offset == strlen(tmpbuf)+1)
												{ /* All of [buffer] has been sent */

													wm_sprintf( info, "\r\nhttp_ClientTestDataHandler: %d bytes written\r\n", offset);

													wip_debug(info);
												}
										// wip_shutdown( http_ClientTestCtx.DataChannel,FALSE,TRUE);
												adl_memRelease (tmpbuf);
												tmpbuf = NULL;

												wip_debug( "\r\nTMPBUFF is freed\r\n" );
												break;

		case WIP_CEV_PEER_CLOSE: wip_debug("\r\nhttp_ClientTestDataHandler: 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, strlen(tmpbuf),WIP_COPT_END) == OK)
												{
													ascii sbuf[16];
													wip_debug("http_ClientTestDataHandler: Status=" );
													wm_sprintf( sbuf, "%d", status);
													wip_debug(sbuf);
													wip_debug( "\r\nhttp_ClientTestDataHandler: Reason=\"" );
													wip_debug(tmpbuf);
													wip_debug( "\"\r\n" );
												}
												if( wip_getOpts( ev->channel,WIP_COPT_HTTP_HEADER, "content-type", tmpbuf, strlen(tmpbuf),WIP_COPT_END) == OK)
												{
													wip_debug("http_ClientTestDataHandler: Content Type=\"%s\"\n", tmpbuf);
												}
												wip_debug("http_ClientTestDataHandler: Response Length=%d bytes\n",pHttpClientCtx->dataLength );

												// data channel must be closed

												wip_close( ev->channel);

												break;

		case WIP_CEV_ERROR: wip_debug("http_ClientTestDataHandler: WIP_CEV_ERROR %d\n",ev->content.error.errnum);
												//adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: ERROR\r\n" );
												wip_debug( "\r\nhttp_ClientTestDataHandler: ERROR\r\n");
												// connection to server broken
												wip_close( ev->channel);
												break;

		default: wip_debug("http_ClientTestDataHandler: unexpected event: %d\n",ev->kind);
												wip_debug("\r\nunexpected event\r\n");
												break;
	}
}

 s32 http_Client(void)
{
	s32 ret = 0;
	char conlen[3];

	// HTTP Session creation
	// ---------------------------
	wm_sprintf(conlen,"%03d",strlen(Data_PCKT)+3);
	wip_debug("\nCONLEN = %s\n",conlen);

	wip_debug( "\nCX Channel State before Initialisation....\n" );
	switch(wip_getState(http_ClientTestCtx.CnxChannel))
	{
		case WIP_CSTATE_BUSY: wip_debug( "\nCX Channel Busy....\n" );
								break;
		case WIP_CSTATE_READY: wip_debug( "\nCX Channel Open and Ready...\n" );

								wip_close(http_ClientTestCtx.CnxChannel);
								http_Client();
								return 0;
		case WIP_CSTATE_TO_CLOSE: wip_debug( "\nCX Channel Ready to close....\n" );
								break;

	}
//	http_ClientTestCtx.CnxChannel = NULL;
	http_ClientTestCtx.CnxChannel = wip_HTTPClientCreateOpts(	NULL, // no handler
																NULL, // no context
																// default headers
																WIP_COPT_HTTP_HEADER, "User-Agent", "HTTP/1.1",
																WIP_COPT_END);
	if (http_ClientTestCtx.CnxChannel == NULL)
	{
		wip_debug( "\ncannot create http session channel\n" );
		//adl_atSendResponse ( ADL_AT_UNS, "cannot create http session channel\r\n" );
		wip_debug("\r\ncannot create http session channel\r\n");
		ret = -1;
	}
	else
	{
		wip_debug( "\r\nhttp session channel created\r\n");

		// HTTP POST command
// http_ClientTestCtx.DataChannel = NULL;
		http_ClientTestCtx.DataChannel = wip_getFileOpts(
															http_ClientTestCtx.CnxChannel, // session channel
															HTTP_STR_URL, // URL to receive the data
															http_ClientTestDataHandler, // data handler
															&http_ClientTestCtx, // context
															WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
															// request headers
															WIP_COPT_HTTP_HEADER, "Content-Type", "application/x-www-form-urlencoded",
															WIP_COPT_HTTP_HEADER, "Content-Length", conlen,
															WIP_COPT_HTTP_HEADER, "Accept", "text/html", WIP_COPT_END);
		if (http_ClientTestCtx.DataChannel == NULL)
		{
			wip_debug( "cannot create http data channel\n" );
			wip_debug( "\r\ncannot create http data channel\r\n");
			wip_close( http_ClientTestCtx.CnxChannel);
			// ret =-1;
		}
		else
			wip_debug( "\r\nHTTP data channel created\r\n");
	}

	return(ret);
}
```

---

<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: [January 22, 2011, 10:15pm UTC](https://forum.sierrawireless.com/t/wip-http-post-problems/4220/6 "2011-01-22T22:15:25Z")

</div>

> [@Vamsi457](#):
>
> ```
> I was able to send data through HTTP post method only once. For second time I am getting HTTP 400 Error Response. 
> 
> ```

> [@Http failed the second time](https://forum.sierrawireless.com/t/http-failed-the-second-time/4554/1):
>
> good afternoon everybody. Could you help me for a little problem. i’m trying to send a http get request to a http server. The first time i send the request, i correctly have my answer but the second time, i have a WIP\_CERR\_ABORTED error. if i try to send my request a third time i have my answer. it works one time on two. Here is a part of my code. // ========================================================================= // CLASSE : // METHODE : httpGetDataHandler // =======================…
