HTTP Error Adding Headers

Hello,

I am using the core http library and I am having some trouble with adding header to my message. The problem occurs when I am trying to add more than 5 headers. The order of the headers does not matter, the 6th always gets lost.

It seems like an overflow, but I have given my program more than enough stack and it has the same behaviour. The HTTPClient_AddHeader is not throwing any errors. The way I save this error was by printing out the header buffer.

User-Agent: my-platform-name

Host: example.com

Connection: keep-alive

Authorization: <KEY>

Content-Type: application/json
<n ▒m ▒ ▒▒▒K
HTTPStatus_t HTTP_request( HttpPackage_t * pHttpPackage )

{
	HTTPStatus_t httpStatus = HTTPSuccess;

	HTTPRequestInfo_t * pHttpRequestInfo = pHttpPackage->pHttpRequestInfo;
	HTTPResponse_t * pHttpResponse = pHttpPackage->pHttpResponse;
	TransportInterface_t * pTransportInterface = pHttpPackage->pTransportInterface;
	char * pRequestBody = pHttpPackage->pRequestBody;
	U8 requestBodyLen = pHttpPackage->requestBodyLen;

	if( !pHttpPackage || !pHttpRequestInfo || !pHttpResponse || !pTransportInterface || 
	    !pRequestBody )
	{
		httpStatus = HTTPInvalidParameter;
	}

	/* Protect buffer with mutex */
    xSemaphoreTake(HTTP_Mutex, portMAX_DELAY);

	HTTPRequestHeaders_t httpRequestHeaders;
	memset( &httpRequestHeaders, 0x00, sizeof(httpRequestHeaders) );
	memset( requestBuffer, 0x00, sizeof(HTTP_BUFFER_LEN) );
	httpRequestHeaders.pBuffer = &requestBuffer;
	httpRequestHeaders.bufferLen = HTTP_BUFFER_LEN;
	
	if( httpStatus == HTTPSuccess )
	{
		LOG_info(("Initializing HTTP Headers"));
		httpStatus = HTTPClient_InitializeRequestHeaders(&httpRequestHeaders, pHttpRequestInfo);
	}
			
	httpStatus = HTTPClient_AddHeader(&httpRequestHeaders, RC_API_HEADER_AUTHORIZATION_KEY, RC_API_HEADER_AUTHORIZATION_KEY_SIZE, 
										RC_API_HEADER_AUTHORIZATION_VALUE, RC_API_HEADER_AUTHORIZATION_VALUE_SIZE);
	httpStatus = HTTPClient_AddHeader(&httpRequestHeaders, RC_API_HEADER_CONTENT_TYPE_KEY, RC_API_HEADER_CONTENT_TYPE_KEY_SIZE, 
										RC_API_HEADER_CONTENT_TYPE_VALUE, RC_API_HEADER_CONTENT_TYPE_VALUE_SIZE);
	httpStatus = HTTPClient_AddHeader(&httpRequestHeaders, RC_API_HEADER_CONTENT_LENGTH_KEY, RC_API_HEADER_CONTENT_LENGTH_KEY_SIZE, 
										RC_API_HEADER_CONTENT_LENGTH_VALUE, RC_API_HEADER_CONTENT_LENGTH_VALUE_SIZE);
	
	if( httpStatus != HTTPSuccess )
	{
		LOG_error(("HTTP Header issue"));	
	}

	if( httpStatus == HTTPSuccess )
	{
		LOG_info(("Sending HTTP request %s", httpRequestHeaders.pBuffer));
		httpStatus = HTTPClient_Send(pTransportInterface, &httpRequestHeaders, ( uint8_t * ) pRequestBody,
                                     requestBodyLen, pHttpResponse, pHttpRequestInfo->reqFlags);
	}

	/* Free mutex */	
	xSemaphoreGive(HTTP_Mutex);

	if( httpStatus != HTTPSuccess && pHttpRequestInfo)
	{
		LOG_error(("%.*s failed HTTP Request", pHttpRequestInfo->hostLen, pHttpRequestInfo->pHost));
	}

	LOG_debug(("RESP: %.*s", pHttpResponse->bodyLen, pHttpResponse->pBody));

	return httpStatus;

}

Any thoughts would be appreciated! I can’t see anything wrong at the moment that would be causing this. My buffer size is at 2Kb at the moment which is way more than I need.

Hey, we’re looking into this.

Have you configured the libraries logging?

Can you try increasing HTTP_BUFFER_LEN?

Hi, yes I increased my buffer from 512 to 2048 and still get the same behaviour. It is very strange. I will continue to try and debug this today. Any more ideas are appreciated.

I do have logging, although I have not seen any errors in the logs. I will grab some and post them today.

I have found the issue. Thankfully it was a mistake on my part. My print buffer was actually too small for the header message. My print buffer was set to 256. I solved this by increasing this buffer.

Thank you for reporting back!