Re: Using Curl to send a HTTP request to a local OSRM Server from an AGL service

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Sebastian,


If you don't mind to use C interface for CURL here are the steps:

1) #include <curl/curl.h> in your service code

2) you are going to receive JSON, so #include <json-c/json.h> also

3) Implement callback for CURL which will take a payload of the answer to your request and make JSON object out of it, like this


static size_t json_download_cb(char *ptr, size_t size, size_t nmemb, void *usr)
{
size_t realsize = size * nmemb;
struct json_object** output = (struct json_object**)usr;
*output = json_or_null(ptr, realsize);
return realsize;
}

Note that you are going to return your JSON object via usr pointer

implement json_or_null function like this:

static struct json_object* json_or_null(const char* body, size_t len)
{
struct json_object* result;
// the only reason for using _ex is null termination which is not the case for curl body
struct json_tokener* tok = json_tokener_new();
result = json_tokener_parse_ex(tok, body, (int)len);
json_tokener_free(tok);
return result;
}

and then do something like:
struct json_object* download_json(const char* link)
{
CURL* curl_handle = curl_easy_init();
struct json_object* result = NULL;

curl_easy_setopt(curl_handle, CURLOPT_URL, link);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, json_download_cb);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 500);
(void)curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);

return result;
}

And voila, you can call this download_json("your_url_here") by timer or whatever way you prefer.
After you received your JSON, you can pass it to GUI part of your application via 
afb_event_broadcast(<name_of_your_event>, <your_json>); or something like.

Don't forget to call curl_global_init(CURL_GLOBAL_ALL); at the beginning of your service's life.

Hope this helps.

BRs,
Denis.








От: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx <agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx> от имени sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Отправлено: 25 мая 2021 г. 13:59:02
Кому: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx
Тема: Using Curl to send a HTTP request to a local OSRM Server from an AGL service
 
Hello everyone,

my name is Sebastian, I'm from Germany and studying at the Reutlingen University. A few weeks ago I visited one of your AGL calls to find out how to best interact with a local OSRM (Open Source Routing Machine) server from an AGL service. The goal of my service is to retrieve routing information from the OSRM server, based on given start und destination points and return said information to a navigation application which a few of my fellow students are developing.

I initially wanted to use an OSRM c++ library to get the needed Information from the OSRM server but I have'nt been to get that to work.
I now want to use Curl to send a http request to the OSRM server and get a GeoJSON response. I wanted to ask if any of you have any tips or pointers on using Curl in an AGL service to achieve this? Here is an example of a OSRM http request:

http://localhost:5000/route/v1/driving/{Long1},{Lat1};{Long2},{Lat2}?steps=true&geometries=geojson

with {Long1/2} and {Lat1/2} being the longitude and latitude of the start and destination coordinates.

Any Advice would be much appreciated! 

Thank you and best regards

Sebastian



This e-mail and any attachment(s) are intended only for the recipient(s) named above and others who have been specifically authorized to receive them. They may contain confidential information. If you are not the intended recipient, please do not read this email or its attachment(s). Furthermore, you are hereby notified that any dissemination, distribution or copying of this e-mail and any attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by replying to this e-mail and then delete this e-mail and any attachment(s) or copies thereof from your system. Thank you.
_._,_._,_

Links:

You receive all messages sent to this group.

View/Reply Online (#9199) | Reply To Group | Reply To Sender | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [list-automotive-discussions82@xxxxxxxxxxx]

_._,_._,_

[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux