cancel
Showing results for 
Search instead for 
Did you mean: 

how to use httpc_get_file

Lmali.1
Associate III

Hi

I want to download file from HTTP using httpc_get_file

Is there any example how to use it?

I don't know what to put in each variable.

err_t httpc_get_file(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings, altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection)

PS

If you have any other working way to download file from http, I will like to know...

Thanks,

Lior

2 REPLIES 2
Cedric Boudinet
Associate III

Hello

Here an example on how to use this function.

To start the download:

httpc_connection_t http_settings;
httpc_state_t *connection;
void try_http_conn()
{
	ip4_addr_t host;
	IP4_ADDR(&host, 192,168,0,2);
	http_settings.use_proxy =0;
	http_settings.headers_done_fn = NULL;
	http_settings.result_fn = my_httpc_result_fn;
	httpc_get_file(&host, 8080, "/index.html", &http_settings, my_http_cb, 0, &connection);
}

Then you have to declare the callbacks

err_t my_http_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
	printf("tcp_recv_fn:\n");
	for(uint16_t i=0;i<p->len;i++)
		printf("%c", ((char*) p->payload)[i]);
	printf("\n");
	return ERR_OK;
}
 
void my_httpc_result_fn(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err)
{
	printf("my_httpc_result_fn %d %ld %ld\n", httpc_result, rx_content_len, srv_res);
        // if the download succeeds srv_res should be 200, httpc_result 0 (HTTPC_RESULT_OK) and rx_content_len!=0
}

In my_http_cb one should handle the payload to retrieve the download content.

Hope it helps.

Bests

It seems that one should also dereference the packet buffer before returning from the http callback:

pbuf_free(p);