# HTTP Error Adding Headers

**URL:** <https://forums.freertos.org/t/http-error-adding-headers/18238>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [September 22, 2023, 8:39pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238 "2023-09-22T20:39:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drowsell](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@drowsell](https://forums.freertos.org/u/drowsell)\
**Post date:** [September 22, 2023, 8:39pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/1 "2023-09-22T20:39:16Z")

</div>

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.

```auto
User-Agent: my-platform-name

Host: example.com

Connection: keep-alive

Authorization: <KEY>

Content-Type: application/json
<n ▒m ▒ ▒▒▒K

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![archigup](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/archigup/32/1418_2.png) [@archigup](https://forums.freertos.org/u/archigup)\
**Post date:** [September 22, 2023, 11:42pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/2 "2023-09-22T23:42:34Z")

</div>

Hey, we’re looking into this.

Have you configured the libraries logging?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 25, 2023, 4:58am UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/3 "2023-09-25T04:58:10Z")

</div>

> [@drowsell](#):
>
> ```auto
> memset( &httpRequestHeaders, 0x00, sizeof(httpRequestHeaders) );
> memset( requestBuffer, 0x00, sizeof(HTTP_BUFFER_LEN) );
> httpRequestHeaders.pBuffer = &requestBuffer;
> httpRequestHeaders.bufferLen = HTTP_BUFFER_LEN;
> 
> ```

Can you try increasing `HTTP_BUFFER_LEN`?

---

<div class="post-metadata">

**Author:** ![drowsell](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@drowsell](https://forums.freertos.org/u/drowsell)\
**Post date:** [September 25, 2023, 2:26pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/4 "2023-09-25T14:26:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![drowsell](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@drowsell](https://forums.freertos.org/u/drowsell)\
**Post date:** [September 25, 2023, 2:27pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/5 "2023-09-25T14:27:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drowsell](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@drowsell](https://forums.freertos.org/u/drowsell)\
**Post date:** [September 25, 2023, 6:08pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/6 "2023-09-25T18:08:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 25, 2023, 6:16pm UTC](https://forums.freertos.org/t/http-error-adding-headers/18238/7 "2023-09-25T18:16:17Z")

</div>

Thank you for reporting back!
