Hello, I am attempting to use cURL to send a POST request to my web server, yet I am running into many many many issues. First, it took me a minute to figure out what I though I needed to do. I moved the CURL folder from the 'Include' folder into my MSVC project and I compiled libcurl moved it into my project. I have define CURL_STATICLIB in preprocessor.
#include <conio.h>
#include "stdafx.h"
#include "curl\curl.h"
#include <Windows.h>
#include <WinSock2.h>
#pragma comment (lib, "ws2_32.lib")
#pragma comment(lib, "libcurl.lib")
int main()
{
CURL *cUrl;
CURLcode res;
/* Initialize all things with cUrl */
curl_global_init(CURL_GLOBAL_ALL);
/* Get cUrl Hangle */
cUrl = curl_easy_init();
if (cUrl)
{
/* Set URL for us to $POST to */
curl_easy_setopt(cUrl, CURLOPT_URL, "http://jimmyonhentai.x10host.com/");
curl_easy_setopt(cUrl, CURLOPT_POSTFIELDS, "name=Hello&extra=, World");
/* Preform the request. */
res = curl_easy_perform(cUrl);
if (res != CURLE_OK)
printf("Whoops! Failed to perform! %s\n", curl_easy_strerror(res));
curl_easy_cleanup(cUrl);
}
curl_global_cleanup();
printf("Done!");
getch();
return 0;
}
Errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Severity Code Description Project File Line Suppression State
Error LNK1120 16 unresolved externals curltest c:\users\justin\documents\visual studio 2017\Projects\curltest\Debug\curltest.exe 1
Error LNK2019 unresolved external symbol __imp__ber_free referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_bind_s referenced in function _ldap_win_bind curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_err2string referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_first_attribute referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_first_entry referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_get_dn referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_get_values_len referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_init referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_memfree referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_msgfree referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_next_attribute referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_next_entry referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_search_s referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_set_option referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_unbind_s referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
Error LNK2019 unresolved external symbol __imp__ldap_value_free_len referenced in function _Curl_ldap curltest c:\Users\Justin\documents\visual studio 2017\Projects\curltest\curltest\libcurl.lib(ldap.obj) 1
These unresolved externals are for LDAP functions.
Try adding this: #pragma comment ( lib, "wldap32.lib" ) // implicitly link to the windows ldap library
Alternatively, since you do not appear to need LDAP functionality, you can build libCURL without LDAP support.
Preprocessor: in addition to defining CURL_STATICLIB also define CURL_DISABLE_LDAP
Edit:
Sorry to ask two questions in one thread but is there any way to store the return from the web server? I know preform() will print what the web server echoes but can I store what it echoes to and strcmp() it?
// consume the data received by curl (append to the string)
staticint append_to_string( char* recd_data, size_t size, size_t nmemb, std::string* ptr_string )
{
const std::size_t nbytes = size * nmemb ;
if( ptr_string && recd_data && nbytes )
{
ptr_string->append( recd_data, nbytes ) ;
return nbytes ;
}
elsereturn 0 ;
}
And then:
1 2 3 4 5
std::string recd_string ; // string to hold the bytes returned by the server
std::string* ptr_string = std::addressof(recd_string) ; // address of the string
curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, append_to_string ); // set the write callback function
curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, ptr_string ); // to which this pointer is to be passed as the last argument
std::string recd_string; // string to hold the bytes returned by the server
std::string* ptr_string = std::addressof(recd_string); // address of the string
staticint append_to_string(char* recd_data, size_t size, size_t nmemb, std::string* ptr_string)
{
const std::size_t nbytes = size * nmemb;
if (ptr_string && recd_data && nbytes)
{
ptr_string->append(recd_data, nbytes);
return nbytes;
}
elsereturn 0;
}
int main()
{
CURL *cUrl;
CURLcode res;
/* Initialize all things with cUrl */
curl_global_init(CURL_GLOBAL_ALL);
/* Get cUrl Hangle */
cUrl = curl_easy_init();
if (cUrl)
{
/* Set URL for us to $POST to */
curl_easy_setopt(cUrl, CURLOPT_URL, "http://jimmyonhentai.x10host.com/script.php");
curl_easy_setopt(cUrl, CURLOPT_POSTFIELDS, "name=Hello&extra=, World");
/* Set up to store response */
curl_easy_setopt(cUrl, CURLOPT_WRITEFUNCTION, append_to_string); // set the write callback function
curl_easy_setopt(cUrl, CURLOPT_WRITEDATA, ptr_string); // to which this pointer is to be passed as the last argument
/* perform */
res = curl_easy_perform(cUrl);
if (res != CURLE_OK)
printf("Whoops! Failed to perform! %s\n", curl_easy_strerror(res));
curl_easy_cleanup(cUrl);
}
curl_global_cleanup();
printf("\nDone!\nOutput: %s", recd_string);
getch();
return 0;
}
Tip: enforce conformance to C++ to the extent possible and enable warnings;
the compiler would alert you of many possible errors if you do.
Microsoft (warning):
source_file.cpp(10): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'std::string'
main.cpp:10:41: error: cannot pass non-trivial object of type 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]
std::printf( "\nDone!\nOutput: %s", recd_string ) ;
~~ ^~~~~~~~~~~
main.cpp:10:41: note: did you mean to call the c_str() method?
std::printf( "\nDone!\nOutput: %s", recd_string ) ;
^
.c_str()
1 error generated.
main.cpp: In function 'int main()':
main.cpp:10:53: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' [-Wformat=]
std::printf( "\nDone!\nOutput: %s", recd_string ) ;
^