Hi,
I’m running into a weird bug with http_client. If I try to write the response to a file, it crashes out. Here’s the callback:
static void get_message_response_cb(struct http_response *rsp,
enum http_final_call final_data,
void *user_data)
{
ssize_t output = 0;
int ret;
if (rsp->body_found)
{
output = fs_write(&file, rsp->body_frag_start, rsp->body_frag_len);
//output = fs_write(&file, "hello", 5);
if (output <= 0) {
LOG_INF("output: %d", output);
}
}
}
The weird thing is that the program will log output: 0
, but write data to the file anyway, but just once! Then there will be just one subsequent call to the callback function, which will have http status of 0.
If I comment out the first call to fs_write
and uncomment the second, then print the fill contents out, it will have hello
in there several times, which is expected since the file I’m downloading is longish.
Any idea what’s going on here?
Here’s the function that kicks off the request in case its helpful:
int get_message(char* file_name)
{
int fd = -1;
int ret = 0;
uint8_t recv_buf_ipv4[BUFFER_SIZE] = {0};
// open file for recording response
fs_file_t_init(&file);
ret = fs_open(&file, file_name, FS_O_CREATE | FS_O_APPEND);
if (ret < 0)
{
LOG_ERR("FAIL: open %s: %d", file_name, ret);
}
LOG_INF("Getting Message From: %s", CONFIG_CLOUD_HOSTNAME);
/* Setup socket */
fd = socket_setup();
if (fd < 0)
{
LOG_ERR("Unable to setup socket. Err: %i", ret);
return ret;
}
LOG_INF("Socket setup complete");
struct http_request req;
memset(&req, 0, sizeof(req));
//MAYBE? change to old header?
char *const headers[] = {
//"Transfer-Encoding: chunked\r\n",
"Connection: close\r\n",
NULL
};
char * payload = "empty";
req.method = HTTP_GET;
req.url = "/api/messages/1/1/500";
req.host = CONFIG_CLOUD_HOSTNAME;
req.protocol = "HTTP/1.1";
req.payload = payload;
req.payload_len = strlen(payload);
req.header_fields = (const char **)headers;
req.response = get_message_response_cb;
req.recv_buf = recv_buf_ipv4;
req.recv_buf_len = sizeof(recv_buf_ipv4);
//req.content_type_value = "application/json";
ret = http_client_req(fd, &req, timeout, NULL);
if (ret < 0)
LOG_ERR("Unable to send data to cloud. Err: %i", ret);
/* Close connection */
(void)close(fd);
LOG_INF("closing file");
fs_close(&file);
return 0;
}
thank you!
Jason