Other Parts Discussed in Thread: SYSBIOS
Hello,
I have troubles with HTTP client. One of them is how to properly handle `HTTPClient_ERESPONSEINVALID (-3003)` error - I guess not only this one. I get this error calling the `HTTPClient_sendRequest()` function. Well, happens I guess, but the problem is that I call `HTTPClient_disconnect()` right after and I ends up with `ti_sysbios_family_arm_m3_Hwi_excHandlerAsm__I` and the app hangs.
My question is how to handle properly errors from HTTP client or sockets, to clear everything and try again.
My code is below:
int http_request(
const char *uri, //!< URI (e.g. https://host.com:8080/entry).
const char *method, //!< Method (e.g. GET, POST, ...).
http_header_t *headers, //!< Additional request headers.
int headers_length, //!< Additional request headers length.
const char *request_body, //!< HTTP request body.
int request_body_length, //!< HTTP request body length.
char *response_body, //!< HTTP response body.
int *response_body_length) //!< HTTP response body length.
{
#define HOSTNAME_LENGTH 64
char hostname[HOSTNAME_LENGTH];
HTTPClient_Handle handle;
HTTPClient_extSecParams sec = {0};
bool disconnect_needed = false;
bool has_more_data = true;
int body_length = 0;
uint16_t status_code = 0;
#ifdef HTTP_DEBUG
const char *debug_data = response_body;
Display_printf(display, 0, 0, "HTTP %s %s", method, uri);
#endif
(*response_body_length)--; // save space for null
int16_t error = _http_get_hostname_from_uri(uri, hostname, sizeof(hostname));
if (error >= 0)
{
handle = HTTPClient_create(&error, 0);
}
if (error >= 0)
{
uri += error;
error = HTTPClient_setHeader(handle, HTTPClient_HFIELD_REQ_USER_AGENT, HTTP_USER_AGENT,
sizeof(HTTP_USER_AGENT), 0);
}
while ((error >= 0) && (headers != NULL) && (headers_length-- > 0))
{
if (headers->header[0] != 0)
{
error = HTTPClient_setHeaderByName(handle, HTTPClient_HFIELD_REQ_AUTHORIZATION,
headers->header, (char *)headers->value, strlen(headers->value) + 1, 0);
}
headers++;
}
#ifdef HTTP_DEBUG
Display_printf(display, 0, 0, "HTTP request body %s", request_body);
#endif
if (error >= 0)
{
error = HTTPClient_connect(handle, hostname, HTTP_IS_SEC(hostname) ? &sec : NULL, 0);
}
if (error >= 0)
{
disconnect_needed = true;
error = HTTPClient_sendRequest(handle, method, (*uri != 0) ? uri : "/", request_body,
request_body_length, 0);
}
if (error >= 0)
{
status_code = error;
// TODO: can we disconnect without getting all data?
while ((*response_body_length > 0) && has_more_data)
{
error = HTTPClient_readResponseBody(handle, response_body, *response_body_length,
&has_more_data);
if (error > 0)
{
response_body += error;
*response_body_length -= error;
body_length += error;
}
else
{
*response_body = 0;
break;
}
}
}
#ifdef HTTP_DEBUG
Display_printf(display, 0, 0, "HTTP error %d", error);
#endif
if (disconnect_needed)
{
HTTPClient_disconnect(handle);
}
#ifdef HTTP_DEBUG
Display_printf(display, 0, 0, "HTTP disconnected");
#endif
if (handle)
{
HTTPClient_destroy(handle);
}
*response_body_length = body_length;
#ifdef HTTP_DEBUG
if (error >= 0)
{
Display_printf(display, 0, 0, "HTTP request %d - %d", status_code, error);
_http_debug_print_buffer(debug_data, body_length);
}
else
{
Display_printf(display, 0, 0, "HTTP request failed %d", error);
}
#endif
return (error < 0) ? error : status_code;
}