Last active
May 25, 2022 14:31
-
-
Save yurynix/08899f4a95f315b6dd5bd7010b6c1e50 to your computer and use it in GitHub Desktop.
Get HEAD status code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc curl.c -lcurl | |
#include <stdio.h> | |
#include <curl/curl.h> | |
// returns: | |
// 0 - file does not exists | |
// 1 - file exists | |
// -1 - failure | |
int is_remote_file_exists(const char* url) { | |
CURL *curl = curl_easy_init(); | |
if (curl) { | |
/* First set the URL that is about to receive our POST. This URL can | |
just as well be a https:// URL if that is what should receive the | |
data. */ | |
curl_easy_setopt(curl, CURLOPT_URL, url); | |
/* get us the resource without a body - use HEAD! */ | |
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); | |
/* Perform the request */ | |
CURLcode res = curl_easy_perform(curl); | |
if (res != CURLE_OK) { | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", | |
curl_easy_strerror(res)); | |
return -1; | |
} | |
long http_code = 0; | |
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); | |
printf("status code: %ld\n", http_code); | |
/* always cleanup */ | |
curl_easy_cleanup(curl); | |
return http_code == 200 ? 1 : 0; | |
} | |
return -1; | |
} | |
int main(void) { | |
/* In windows, this will init the winsock stuff */ | |
curl_global_init(CURL_GLOBAL_ALL); | |
int res = is_remote_file_exists("http://example.com/sdgsdgsd"); | |
printf(res > 0 ? "exists\n" : "not exists or failure\n"); | |
curl_global_cleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment