\chapter{Ecore\+\_\+\+Con\+\_\+\+Url -\/ customizing a request}
\hypertarget{ecore_con_url_headers_example_c}{}\label{ecore_con_url_headers_example_c}\index{Ecore\_Con\_Url -\/ customizing a request@{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 \doxylink{ecore_con_url_headers_example_8c-example}{ecore\+\_\+con\+\_\+url\+\_\+headers\+\_\+example.\+c}.

The first part of the example is setting the callbacks to be called when an \doxylink{group__Ecore__Con__Events__Group_ga5affbb43768cd629c629d80afa2b5706}{ECORE\+\_\+\+CON\+\_\+\+EVENT\+\_\+\+URL\+\_\+\+DATA} or \doxylink{group__Ecore__Con__Events__Group_gaa814f51fd5f97a92cdc76b624651d85a}{ECORE\+\_\+\+CON\+\_\+\+EVENT\+\_\+\+URL\+\_\+\+COMPLETE} event is received. These are the callbacks that are going to be used with this\+:

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_data\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Data}{Ecore\_Con\_Event\_Url\_Data}}\ *url\_data\ =\ event\_info;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ url\_data-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Data_a1d4725aa77f3875cc3405597a5fbff91}{size}};\ i++)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%c"{}},\ url\_data-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Data_aaa4c4dbc6575b071164f8152a7808268}{data}}[i]);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_complete\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete}{Ecore\_Con\_Event\_Url\_Complete}}\ *url\_complete\ =\ event\_info;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *headers,\ *l;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}download\ completed\ with\ status\ code:\ \%d\(\backslash\)n"{}},\ url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_ab842caa1615a911c3578e0dfeae5f773}{status}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ headers\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga688c91a2eb687133c245c5b0fb4f2219}{ecore\_con\_url\_response\_headers\_get}}(url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(headers,\ l,\ str)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}header:\ \%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The {\ttfamily 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\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ Ecore\_Con\_Url\ *ec\_url\ =\ NULL;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *type,\ *post\_data\ =\ \textcolor{stringliteral}{"{}"{}};}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}\ r;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (argc\ <\ 3)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}need\ at\ least\ two\ parameters:\ <\ POST|GET\ >\ \ <url1>\ [post-\/data]\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Then we start our required libraries and configure a global option to use pipelined requests\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ type\ =\ argv[1];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (strcmp(type,\ \textcolor{stringliteral}{"{}POST"{}})\ \&\&\ (strcmp(type,\ \textcolor{stringliteral}{"{}GET"{}})))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}only\ POST\ or\ GET\ are\ supported\ by\ this\ example.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (argc\ >\ 3)\ post\_data\ =\ argv[3];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga8a20f47372627a03e86a8705a6f8a82d}{ecore\_con\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga96388997453269f58bde7873811af6d5}{ecore\_con\_url\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ check\ if\ requests\ are\ being\ pipelined,\ and\ set\ them\ if\ not:}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Ecore__Con__Url__Group_gaebd8dbd1359502c6a6b92267797a3339}{ecore\_con\_url\_pipeline\_get}}())}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaa13943c00d041db2391e1d5a04600db2}{ecore\_con\_url\_pipeline\_set}}(\mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}

\end{DoxyCodeInclude}
 Now we create our request object, but using \doxylink{group__Ecore__Con__Url__Group_gab6361a63154fa2ac91c9a963ee388455}{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\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ ec\_url\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gab6361a63154fa2ac91c9a963ee388455}{ecore\_con\_url\_custom\_new}}(argv[2],\ type);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!ec\_url)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}error\ when\ creating\ ecore\ con\ url\ object.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ end;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_ga5affbb43768cd629c629d80afa2b5706}{ECORE\_CON\_EVENT\_URL\_DATA}},\ \_url\_data\_cb,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_gaa814f51fd5f97a92cdc76b624651d85a}{ECORE\_CON\_EVENT\_URL\_COMPLETE}},\ \_url\_complete\_cb,\ NULL);}

\end{DoxyCodeInclude}
 In order to demonstrate our API, some options are set to this request before actually performing it\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gae8aba3432937e59e434ea31daf0963b8}{ecore\_con\_url\_additional\_header\_add}}(ec\_url,\ \textcolor{stringliteral}{"{}User-\/Agent"{}},\ \textcolor{stringliteral}{"{}blablabla"{}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gadfdedb767b05247a0eb3660de56151c7}{ecore\_con\_url\_verbose\_set}}(ec\_url,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaa9ada099dbaa8d6ecc21f3d2c0936ced}{ecore\_con\_url\_httpauth\_set}}(ec\_url,\ \textcolor{stringliteral}{"{}user"{}},\ \textcolor{stringliteral}{"{}password"{}},\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gac31f028ff6ce1504971d6267c270b4ea}{ecore\_con\_url\_time}}(ec\_url,\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gga7fd1cb32a295b11143fb1660dc8bcd6bad3aba66e3ab06bb1c66942641a0b2d29}{ECORE\_CON\_URL\_TIME\_IFMODSINCE}},\ 0);}

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


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!strcmp(type,\ \textcolor{stringliteral}{"{}GET"{}}))}
\DoxyCodeLine{\ \ \ \ \ r\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaeb79adc8f07e54ad7a87a8c55de5ec72}{ecore\_con\_url\_get}}(ec\_url);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ r\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga8880afc509b97fbcb4db5ce98b4f4023}{ecore\_con\_url\_post}}(ec\_url,\ post\_data,\ strlen(post\_data),\ NULL);}

\end{DoxyCodeInclude}
 After that, we just check for errors, start the main loop, free resources and finally exit\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!r)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}could\ not\ realize\ request.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ free\_ec\_url;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{free\_ec\_url:}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga856c091a11980598adb0666cec9b5add}{ecore\_con\_url\_free}}(ec\_url);}
\DoxyCodeLine{end:}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaabcdae26e4a474dc5b9c43f60e4d00d4}{ecore\_con\_url\_shutdown}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga0e5e01c67f7135e16db0e8f17d087d99}{ecore\_con\_shutdown}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
