HTTP request using CURL API and Ecore Timer

Introduction

Libcurl is an open source library to implement URL-related transfer activities without a Web browser. Tizen supports this library through the Curl API. Using this API you can simply do HTTP requests with various options.

On the other hand, Ecore provides very flexible timer functionality. The basic usage of timers is to call a certain function at a certain interval. You can make all the functionalities to perform within that certain interval using simple callback functions registered within the timer.

In this tip document, step by step methods are shown on how to create a simple native service application in Tizen using CURL API and Ecore Timer to make HTTP requests at a certain time interval.

Steps to do

Step 1: Add headers for CURL Library

To use the functions and data types of the Curl API, include the following header file in your application.

#include <curl/curl.h>

You must add the following header file to get the proxy address.

#include <net_connection.h>

Step 2: Add header for Ecore Timer

To use the timer functionality provided by Ecore, include the following header in the application.

#include <Ecore.h>

Step 3: Initialization and Prerequisites

The following privileges are required to make the HTTP requests from the application.

Privilege

Description

http://tizen.org/privilege/internet

Allows the application to use the Internet connection.

http://tizen.org/privilege/network.get

Allows the application to manage network connections.

Here is the example code of tizen-manifest.xml which is modified with required privileges:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<manifest xmlns="http://tizen.org/ns/packages" api-version="2.4" pack-age="org.example.service_curl" version="1.0.0">
    <profile name="mobile"/>
    <service-application appid="org.example.service_curl" exec="service_curl" multi-ple="false" nodisplay="true" taskmanage="false" type="capp">
        <label>service_curl</label>
        <icon>service_curl.png</icon>
    </service-application>
    <privileges>
	  <privilege>http://tizen.org/privilege/internet</privilege>
        <privilege>http://tizen.org/privilege/network.get</privilege>
    </privileges>
</manifest>

Step 4: Creating Timer

The ecore_timer_add function creates a timer to call the given function in particular period of time. Here the function sendRequest will be called every 300 seconds and it will be passed NULL as its parameter.

ecore_timer_add(300, sendRequest, NULL);

Step 5: Creating structure and byte allocation

Create a structure and allocate bytes in memory to write the data in it through the code given below:

struct MemoryStruct {
  char *memory;
  size_t size;
};
struct MemoryStruct chunk;

chunk.memory = malloc(1);
chunk.size = 0;  

Step 6: Initializing Curl library and Curl handle

Initialize the Curl library and the Curl handle using the curl_easy_init function to use the functionality of CURL API.

CURL *curl;
CURLcode curl_err;

/* init the curl session */
curl = curl_easy_init();

Step 7: Create and Initialize connection handle

The connection_create function creates a handle for managing data connections. It returns 0 on success, otherwise a negative error value. Create and initialize a connection handle by using the following code structure:

connection_h connection;
int conn_err;
conn_err = connection_create(&connection);
if (conn_err != CONNECTION_ERROR_NONE)
{
	/* Error handling */

	return false;
}

Step 8: Transferring HTTP Requests

To transfer HTTP request, set the URL with curl_easy_setopt function and start the transfer with the curl_easy_perform function.

curl_easy_setopt(curl, CURLOPT_URL, "http://apidev.accuweather.com/currentconditions/v1/28143.json?language=en&apikey=hoArfRosT1215");

/* send all data to this function  */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

/* we pass the 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

/* getting data */
curl_err = curl_easy_perform(curl);

if (curl_err != CURLE_OK) {
    /* Error handling */
	return false;
}

Step 9: Get Data in Log

As service applications do not carry out any UI with it, you can see the output in log (at log tab in your IDE). To get the data in log use the code below:

dlog_print(DLOG_INFO, "data", chunk.memory);

Step 10: Clear the Curl, connection handle and memory

After all the things are done, clear the Curl, destroy the connection handle and make the memory allocated by “malloc” free.

/* cleanup curl stuff */ 
curl_easy_cleanup(curl);
connection_destroy(connection);
free(chunk.memory);

Running the sample application

Now, the sample application is capable of making HTTP requests within a certain time period. Build and run the attached sample application and look in the log (inside your IDE) for data.

Figure: Getting data by making HTTP requests using Curl within every five minutes.

References:

[1] https://curl.haxx.se/libcurl/c/getinmemory.html

[2] https://developer.tizen.org/development/api-tutorials/native-application/network/curl#init

[3] https://developer.tizen.org/dev-guide/native/2.3.0/org.tizen.mobile.native.apireference/group__Ecore__Timer__Group.html

첨부 파일: 
List
SDK Version Since: 
2.4 mobile/2.3.1 wearable