Jarred Parr
Jarred Parr

Reputation: 1257

curl_easy_perform() failed: Couldn't connect to server

So I am using libcurl with C++ to retreive the data in a page, but for some reason it throws the error in the post title when I connect to my vps. The code in the vps just makes a get request and spits out some data from my database.

#include "requestMapper.hpp"

// Send a GET request to API to retreive image
void RequestMapper::retreiveData(std::string url) {
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        ISSUE HERE // curl_easy_setopt(curl, CURLOPT_URL, "https://104.236.200.91/index.php/food/");
        res = curl_easy_perform(curl);
#ifdef SKIP_PEER_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
}

// Submit a post request to API link to send data
void RequestMapper::postData() {

}


int main(void) {
    RequestMapper rm;
    rm.retreiveData("sample");
    return 0;
}

This code is working fine when the url is something like "https://google.com" but when I hit the link to my web server I set up I get that error. I am newer to this library, but I thought I was doing things correctly. if anyone has any insight please let me know! I'll continue to tinker with this and if I find a solution I'll share it here.

Thanks!

EDIT:

I recently solved this issue and wanted to share it with anyone who might stumble upon this post. I was using https://URL when instead I should have been using http://URL so if you run into this issue try using that instead first!

Upvotes: 4

Views: 17168

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 58004

That's error 7, "couldn't connect to host" described in the curl book like this:

Failed to connect to host. curl managed to get an IP address to the machine and it tried to setup a TCP connection to the host but failed. This can be because you have specified the wrong port number, entered the wrong host name, the wrong protocol or perhaps because there is a firewall or another network equipment in between that blocks the traffic from getting through.

Upvotes: 4

Related Questions