MCPcopy Create free account
hub / github.com/antirez/botlib / makeHTTPGETCall

Function makeHTTPGETCall

botlib.c:260–295  ·  view source on GitHub ↗

Request the specified URL in a blocking way, returns the content (or * error string) as an SDS string. If 'resptr' is not NULL, the integer * will be set, by reference, to 1 or 0 to indicate success or error. * The returned SDS string must be freed by the caller both in case of * error and success. */

Source from the content-addressed store, hash-verified

258 * The returned SDS string must be freed by the caller both in case of
259 * error and success. */
260sds makeHTTPGETCall(const char *url, int *resptr) {
261 if (Bot.debug) printf("HTTP GET %s\n", url);
262 CURL* curl;
263 CURLcode res;
264 sds body = sdsempty();
265
266 curl = curl_easy_init();
267 if (curl) {
268 curl_easy_setopt(curl, CURLOPT_URL, url);
269 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, makeHTTPGETCallWriterSDS);
270 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
271 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
272 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
273 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
274 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
275
276 /* Perform the request, res will get the return code */
277 res = curl_easy_perform(curl);
278 if (resptr) *resptr = res == CURLE_OK ? 1 : 0;
279
280 /* Check for errors */
281 if (res != CURLE_OK) {
282 const char *errstr = curl_easy_strerror(res);
283 body = sdscat(body,errstr);
284 } else {
285 /* Return 0 if the request worked but returned a 500 code. */
286 long code;
287 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
288 if ((code == 500 || code == 400) && resptr) *resptr = 0;
289 }
290
291 /* always cleanup */
292 curl_easy_cleanup(curl);
293 }
294 return body;
295}
296
297/* Like makeHTTPGETCall(), but the list of options will be concatenated to
298 * the URL as a query string, and URL encoded as needed.

Callers 1

makeHTTPGETCallOptFunction · 0.85

Calls 2

sdsemptyFunction · 0.85
sdscatFunction · 0.85

Tested by

no test coverage detected