Hello Sebastian,
Regarding code snippets provided recently. Please bear in mind that the callback for JSON downloads is oversimplified and will only work well for short payloads.
Actually CURL documentation says, that it might be called multiple times for single payload. This means that sometimes on first call you will receive a half of your JSON, and on second time another half of it, each half is not a valid JSON :\
You might want to use some pre-allocated buffer plus write pointer in it to receive data.
OR you can let CURL and standard library do all this for you by using NULL as CURLOPT_WRITEFUNCTION and use open_memstream to allocate memory buffer which is writable like a file.
the whole download_json is below. just throw away the callback and you are safe.
struct json_object* download_json(const char* link){CURL* curl_handle = curl_easy_init();char* buf = NULL;size_t buffer_size;FILE* json_filestream = open_memstream(&buf, &buffer_size);
curl_easy_setopt(curl_handle, CURLOPT_URL, link);curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL); // use default fwritecurl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, json_filestream); // file pointer to default fwritecurl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 500);(void)curl_easy_perform(curl_handle);fclose(json_filestream); // now buf and buffer_size are validstruct json_object* result = json_or_null(buf, buffer_size);free(buf); // buffer allocated by open_memstream must be freed as per man(3)curl_easy_cleanup(curl_handle);return result;}BRs,
Denis.
От: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx <agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx> от имени sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <sebastian_alois.eckl@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Отправлено: 25 мая 2021 г. 19:24:54
Кому: agl-dev-community@xxxxxxxxxxxxxxxxxxxxxxxxx
Тема: Re: Using Curl to send a HTTP request to a local OSRM Server from an AGL serviceHello Denis,
thanks alot for your reply, that definitely helps. I'll give it a try.
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 (#9207) | Reply To Group | Reply To Sender | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [list-automotive-discussions82@xxxxxxxxxxx]