libcpr not working properly when using it for HTTP request - c++

I am using libcpr for http requests in Visual Studio 2019 IDE. I downloaded it using vcpkg from microsoft. The sample code below is from cpr github page https://github.com/libcpr/cpr#:~:text=%23include%20%3C,return%200%3B%0A%7D
#include <cpr/cpr.h>
int main(int argc, char** argv) {
cpr::Response r =
cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
cpr::Authentication{"user", "pass", cpr::AuthMode::BASIC},
cpr::Parameters{{"anon", "true"}, {"key", "value"}});
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text; // JSON text string
return 0;
}
This doesn't work! It is giving error "namespace "cpr" has no member "AuthMode". This problem is not with this only. There was some other stuff that gives similar error e.g. https://docs.libcpr.org/advanced-usage.html#https-options:~:text=cpr%3A%3ASslOptions%20sslOpts%20%3D%20cpr%3A%3ASsl(ssl%3A%3ACaBuffer%7B%22%2D%2D%2D%2D%2DBEGIN%20CERTIFICATE%2D%2D%2D%2D%2D%5B...%5D%22%7D)%3B%0Acpr%3A%3AResponse%20r%20%3D%20cpr%3A%3AGet(cpr%3A%3AUrl%7B%22https%3A//www.httpbin.org/get%22%7D%2C%20sslOpts)%3B in this case "CaBuffer" has same issue.
Any help would be appreciated!!
Thanks

Looks to me like a versioning issue. AuthMode exists in the latest header file, but does not exist in the version 1.8 header file, which is presumably what you have.
So, either downgrade your code, or upgrade your installation.
Sample code from version 1.8 is here

Related

Timeout sending HTTPRequest in poco 1.10.1

I have a c++ application that connects to a http server on localhost. The application is running on windows 10. I used to use poco 1.7.8p3 to manage the connection. The library was built with the Visual Studio 15 using the buildwin script shipped with the source and everything has been working fine.
I decided to upgrade poco to version 1.10.1. This time I built the library using CMake.
The code that used to work with version 1.7.8p3 does no longer work with poco 1.10.1. When sending a HTTPRequest from a HTTPClientSession, I get a Poco::TimeoutException. The server never receives the request. Any help solving this problem would be appreciated. Sample code follows below.
HTTPClientSession session
session.setKeepAlive(false);
session.setTimeout(Poco::Timespan(0, 0, 0, 5, 0));
int port = 62300;
session.setPort(port);
session.setHost("localhost");
const std::string uri = "/get_server_config";
HTTPRequest request(HTTPRequest::HTTP_GET, uri, HTTPMessage::HTTP_1_1);
//Generates Poco::TimeoutException
session.sendRequest(request);
auto &sock = session.socket();
const Poco::Timespan ts(10L, 1L);
sock.setReceiveTimeout(ts);
It seems like it is no longer possible to specify the host as "localhost".
Changing session.setHost("localhost"); to session.setHost("127.0.0.1"); solved the problem.

Empty Content-Type in curl_easy_perform GET request

Kindly disregard this issue, the problems seems to lie in werkzeug.
I believe I have detected a problem with Content-Type for GET requests sent using curl_easy_perform, in libcurl version 7.29 (CentOS 7).
The problem is that the HTTP header Content-Type is present (with empty value) in the request:
Content-Type:
In Ubuntu 18.04, with libcurl 7.58, this problem doesn't occur (as expected the header is not present in the request). I've searched Changlogs but without finding anything about this assumed fix. I did find some mention of the very same bug fixed in the PHP-curl library.
So, my question is simply: is it true that this behaviour has been fixed somewhere between 7.29 and 7.58 ?
I just made a test implementation, as short as possible, to see the problem.
This is the entire code:
#include <stdio.h> // fprintf, ...
#include <stdlib.h> // exit
#include <curl/curl.h> // curl
#include <curl/curlver.h> // VERSION
int main(int argC, char* argV[])
{
CURL* curlP;
CURLcode cc;
printf("curl version: %s\n", LIBCURL_VERSION);
curlP = curl_easy_init();
if (curlP == NULL)
{
fprintf(stderr, "curl_easy_init failed: %s\n", curl_easy_strerror(cc));
exit(1);
}
curl_easy_setopt(curlP, CURLOPT_URL, "localhost:1028/accumulate");
cc = curl_easy_perform(curlP);
if (cc != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(cc));
exit(2);
}
curl_easy_cleanup(curlP);
return 0;
}
The code is edited in a file called get.c and I compile it like this:
gcc get.c -o get -lcurl
The test runs in CentOS 7 with libcurl 7.58 (that I installed manually under /usr/local).
I have a simple test program, an accumulator, written in python, listening to port 1028, recording all requests that enters, and that spits it out on receiving a request with the URL path /dump.
Here is the entire sequence:
kz#centos7:curlTest> gcc get.c -o get -lcurl
kz#centos7:curlTest> ./get
curl version: 7.58.0
kz#centos7:curlTest> curl localhost:1028/dump
GET http://localhost:1028/accumulate
Content-Length:
Host: localhost:1028
Accept: */*
Content-Type:
=======================================
kz#centos7:curlTest>
As can be seen in this test, the GET request (sent by get to the accumulator) includes HTTP headers Content-Length and Content-Type without value.
The exact same test in Ubuntu 18.04 looks like this:
kz#xps:libcurlTest> gcc get.c -o get -lcurl
kz#xps:libcurlTest> ./get
curl version: 7.58.0
kz#xps:libcurlTest> curl localhost:1028/dump
GET http://localhost:1028/accumulate
Accept: */*
Host: localhost:1028
=======================================
kz#xps:libcurlTest>
Note the absence of Content-Lengthand Content-Type.
Before implementing this little test program, I made numerous attempts to avoid the Content-Type, by setting it in its slist to "Content-Type:", without space, with one space and with two spaces after the colon. I also tried without colon and of course without adding it to the slist (in which case it comes with the default content-type). I also tried to tell libcurl that the request is a GET, don't remember the details of that call right now.
Now, the error is probably mine, as I have very little experience using libcurl. It's just strange that all is OK in Ubuntu but not in CentOS ...

How to integrate MySQL with codeblocks IDE

I have been searching solution for this problem whole day.Any help would make me grateful. I am using Code::blocks IDE 16.01. I want to connect from my IDE to my MySQL database server. I have tried a lot of options but yet to succeed. I have my MYSQL INSTALLER setup already and have downloaded the mysql connector and mysql server. I am gonna show you the code and the way i have already tried.
This is my simple code
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL* conn;
conn = mysql_init(NULL);
if (mysql_real_connect(conn,"localhost","root","","test",0,NULL,0) !=0)
{
cout << "Succesfully Connected to MySQL database xxxx" << endl;
}
mysql_close(conn);
return 0;
}
I have my MySQL setup in C drive and I have linked that as below
After doing all the work I have been shown the following errors
Please someone help me. Thanks in advance. Please feel free to ask If you guyz need anything to know.
In third step of your CodeBlocks setup you must specify the lib path (and not the include path), as below:
C:\Program Files\MySQL\MySQL Server 8.0\lib .
Then rebuilds your application.

Compiling OCCI on "Oracle linux"

I am trying to develop some app using oracle with OCCI in c++. I installed "oracle linux" and oracle db 12c successfully. I am assuming OCCI is included db as its one of parts so I just tried to compile following code as following further down.
g++ -std=c++11 one.cc -o mm
one.cc
#include <occi.h>
int main(int argc, char* argv[]) {
return 0;}
and the compilation result is:
fatal error: occi.h: no such file or dir..
alright, what should I do? am I forgetting something else? or anyother flags so that link? please provide me any thing you think an answer.
tnx
occi.h: no such file
occi.h is provided by e.g. ``oracle-instantclient-devel´´
Available version are 10.2 and 11.2, see http://rpm.pbone.net : CentOS6, RHEL7, Fedora 17.. 18. Or ask Oracle for other choices.

c++ cgi on linux server

I am pretty new to CGI's and wanted to give them a try. Here is my situation:
My website is hosted on a linux server, I have contacted the hosting company and they said as long as I use the cgi-bin folder there is no problem. So I wrote a very short c++ program that just output some text:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello from c++ cgi";
return 0;
}
Since its a linux server (and I am running Windows) I suppose I needed to compile the source in linux so a installed a virtual machine running Ubuntu. I then compiled the source (tested it in Ubuntu) and placed the binary file in the cgi-bin folder. Then I used a simple html anchor to link to it:
T
But when I click the link on my page I get the "500 Internal Server Error" message. I have also tried to rename the binary file to "cgiTest.cgi" and update the link but still the same problem.
Anybody have any idea where my problem lies?
You forgot to output any HTTP headers (the error log entry would give you a hint).
#include <iostream>
using namespace std;
int main()
{
cout << "Content-Type: text/plain" << endl << endl;
cout << "Hello from c++ cgi";
return 0;
}
Thanks for the help, turns out I had to change the permissions and the hosting server is 32bit while I compiled on 64bit Ubuntu. I now compiled on 32bit Ubuntu and voila!, its running. Thanks to all for the help in solving this issue!