Tizen Native API  7.0
Ecore_Con_Url - customizing a request

This is a simple example that shows how to make a custom request using Ecore_Con_Url. The full source code for this example can be found at ecore_con_url_headers_example::c.

The first part of the example is setting the callbacks to be called when an ECORE_CON_EVENT_URL_DATA or ECORE_CON_EVENT_URL_COMPLETE event is received. These are the callbacks that are going to be used with this:

static Eina_Bool
_url_data_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
{
   Ecore_Con_Event_Url_Data *url_data = event_info;
   int i;

   for (i = 0; i < url_data->size; i++)
     printf("%c", url_data->data[i]);

   return EINA_TRUE;
}

static Eina_Bool
_url_complete_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
{
   Ecore_Con_Event_Url_Complete *url_complete = event_info;
   const Eina_List *headers, *l;
   char *str;

   printf("\n");
   printf("download completed with status code: %d\n", url_complete->status);

   headers = ecore_con_url_response_headers_get(url_complete->url_con);

   EINA_LIST_FOREACH(headers, l, str)
     printf("header: %s\n", str);

   ecore_main_loop_quit();

   return EINA_TRUE;
}

The main code is as simple as the Ecore_Con_Url example. It contains some checks for the arguments to see if a GET or POST request is required:

int
main(int argc, const char *argv[])
{
   Ecore_Con_Url *ec_url = NULL;
   const char *type, *post_data = "";
   Eina_Bool r;

   if (argc < 3)
     {
        printf("need at least two parameters: < POST|GET >  <url1> [post-data]\n");
        return -1;
     }

Then we start our required libraries and configure a global option to use pipelined requests:

   type = argv[1];

   if (strcmp(type, "POST") && (strcmp(type, "GET")))
     {
        printf("only POST or GET are supported by this example.\n");
        return -1;
     }

   if (argc > 3) post_data = argv[3];

   ecore_init();
   ecore_con_init();
   ecore_con_url_init();

   // check if requests are being pipelined, and set them if not:
   if (!ecore_con_url_pipeline_get())
     ecore_con_url_pipeline_set(EINA_TRUE);

Now we create our request object, but using ecore_con_url_custom_new() to use a POST or GET method depending on the command line arguments. And we also add the event handlers for our callbacks:

   ec_url = ecore_con_url_custom_new(argv[2], type);
   if (!ec_url)
     {
        printf("error when creating ecore con url object.\n");
        goto end;
     }

   ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _url_data_cb, NULL);
   ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL);

In order to demonstrate our API, some options are set to this request before actually performing it:

Depending on what kind of request was asked (GET or POST), we use one of the specific functions to perform it:

   if (!strcmp(type, "GET"))
     r = ecore_con_url_get(ec_url);
   else
     r = ecore_con_url_post(ec_url, post_data, strlen(post_data), NULL);

After that, we just check for errors, start the main loop, free resources and finally exit:

   if (!r)
     {
        printf("could not realize request.\n");
        goto free_ec_url;
     }

   ecore_main_loop_begin();

free_ec_url:
   ecore_con_url_free(ec_url);
end:
   ecore_con_url_shutdown();
   ecore_con_shutdown();
   ecore_shutdown();

   return 0;
}