Related
I'm trying to send an image buff with cUrl to the telegram API using c++. Just to know I' m developing on Windows 10.
Here is all I have done.
First I send a photo from my hard drive using curl from terminal using this command:
curl -s -X POST "https://api.telegram.org/bottoken/sendPhoto" -F chat_id=id -F photo="#D:/a.jpg"
On the telegram chat I receive the photo with this command meanwhile on the console in which I did the cUrl request I have a response like this:
{"ok":true,"result":{"message_id":2398,"from":{"id":id,"is_bot":true,"first_name":"botname","username":"stuff"},"chat":{"id":chatid,"first_name":"userdata","username":"userdata","type":"private"},"date":1588929212,"photo":[{"file_id":"reallylongfileid","file_unique_id":"shorterid","file_size":26307,"width":240,"height":320}]}}
If I import this request using the Import tab and selecting raw text in Postman and execute it I have this response:
{
"ok": false,
"error_code": 400,
"description": "Bad Request: wrong HTTP URL specified"
}
If I try to use the postman generated code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
// I added CURLOPT_VERBOSE
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "chat_id");
curl_mime_data(part, "mychatid", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "photo");
curl_mime_data(part, "#D:/a.jpg", CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
I have this response on the console:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: self signed certificate in certificate chain
* Closing connection 0
So I added this option to the code I wrote before:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
and the new response is:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; CN=api.telegram.org
* start date: Mar 24 13:48:17 2020 GMT
* expire date: May 23 16:17:38 2022 GMT
* subjectAltName: host "api.telegram.org" matched cert's "api.telegram.org"
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> POST /botmybottoken/sendPhoto HTTP/1.1
Host: api.telegram.org
Accept: */*
Content-Length: 254
Content-Type: multipart/form-data; boundary=------------------------9b6fc10336ea9470
* We are completely uploaded and fine
* old SSL session ID is stale, removing
< HTTP/1.1 400 Bad Request
< Server: nginx/1.16.1
< Date: Fri, 08 May 2020 09:25:10 GMT
< Content-Type: application/json
< Content-Length: 83
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
<
{"ok":false,"error_code":400,"description":"Bad Request: wrong HTTP URL specified"}* Connection #0 to host api.telegram.org left intact
* Closing connection 0
If I try to send this image buffer:
cv::Mat img(260, 301, CV_8UC3, cv::Scalar(0, 0, 0));
std::vector<uchar> buff;
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 80;
cv::imencode(".jpg", img, buff, param);
std::string strImg((char*)buff.data(), buff.size());
And if I modify the request code only from this:
curl_mime_data(part, "#D:/a.jpg", CURL_ZERO_TERMINATED);
To this:
curl_mime_data(part, strImg.data(), strImg.size());
I have this response in the console:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; CN=api.telegram.org
* start date: Mar 24 13:48:17 2020 GMT
* expire date: May 23 16:17:38 2022 GMT
* subjectAltName: host "api.telegram.org" matched cert's "api.telegram.org"
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> POST /botmybottoken/sendPhoto HTTP/1.1
Host: api.telegram.org
Accept: */*
Content-Length: 2164
Content-Type: multipart/form-data; boundary=------------------------f5bde0e2e0ef8114
Expect: 100-continue
* old SSL session ID is stale, removing
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 400 Bad Request
< Server: nginx/1.16.1
< Date: Fri, 08 May 2020 09:30:34 GMT
< Content-Type: application/json
< Content-Length: 128
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
<
{"ok":false,"error_code":400,"description":"Bad Request: wrong remote file identifier specified: Wrong character in the string"}* Connection #0 to host api.telegram.org left intact
* Closing connection 0
I don't want to use any c++ telegram bot library because I have just to send a Message or a photo.
I' m able to send a message with cUrl code.
With photos, I have some problems.
Because I have to do this two simple actions I prefer to use only cUrl and nothing else.
I need help cause I'm not understanding what's wrong.
I know I write a lot but I wish this could help you to understand everything better and faster.
Thank you!
int sendTelegramPhoto(string chat_id, string path_to_photo, string caption = NULL){
CURL *curl;
CURLcode response;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_slist_append(headers, "Content-Type: multipart/form-data");
curl_slist_append(headers, "charset=utf-8");
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "chat_id");
curl_mime_data(part, chat_id.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "photo");
curl_mime_filedata(part, path_to_photo.c_str());
curl_mime_type(part, "image/jpeg");
part = curl_mime_addpart(mime);
curl_mime_name(part, "caption");
curl_mime_data(part, caption.c_str().c_str(), CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
response = curl_easy_perform(curl);
curl_mime_free(mime);
curl_slist_free_all(headers);
}
curl_easy_cleanup(curl);
return 0;
}
cv::Mat img(260, 301, CV_8UC3, cv::Scalar(0, 0, 0));
std::vector<uchar> buff;
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 80;
cv::imencode(".jpg", img, buff, param);
std::string strImg((char*)buff.data(), buff.size());
CURLcode ret;
CURL* hnd;
curl_mime* mime1;
curl_mimepart* part1;
mime1 = NULL;
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
mime1 = curl_mime_init(hnd);
part1 = curl_mime_addpart(mime1);
curl_mime_data(part1, "mychatid", CURL_ZERO_TERMINATED);
curl_mime_name(part1, "chat_id");
part1 = curl_mime_addpart(mime1);
curl_mime_data(part1, strImg.data(), strImg.size());
curl_mime_filename(part1, "myimage.jpg");
curl_mime_name(part1, "photo");
curl_easy_setopt(hnd, CURLOPT_MIMEPOST, mime1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.87.0-DEV");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
ret = curl_easy_perform(hnd);
...
I'm trying to send an image buff with cUrl to the telegram API using c++. Just to know I' m developing on Windows 10.
Here is all I have done.
First I send a photo from my hard drive using curl from terminal using this command:
curl -s -X POST "https://api.telegram.org/bottoken/sendPhoto" -F chat_id=id -F photo="#D:/a.jpg"
On the telegram chat I receive the photo with this command meanwhile on the console in which I did the cUrl request I have a response like this:
{"ok":true,"result":{"message_id":2398,"from":{"id":id,"is_bot":true,"first_name":"botname","username":"stuff"},"chat":{"id":chatid,"first_name":"userdata","username":"userdata","type":"private"},"date":1588929212,"photo":[{"file_id":"reallylongfileid","file_unique_id":"shorterid","file_size":26307,"width":240,"height":320}]}}
If I import this request using the Import tab and selecting raw text in Postman and execute it I have this response:
{
"ok": false,
"error_code": 400,
"description": "Bad Request: wrong HTTP URL specified"
}
If I try to use the postman generated code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
// I added CURLOPT_VERBOSE
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "chat_id");
curl_mime_data(part, "mychatid", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "photo");
curl_mime_data(part, "#D:/a.jpg", CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
I have this response on the console:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: self signed certificate in certificate chain
* Closing connection 0
So I added this option to the code I wrote before:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
and the new response is:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; CN=api.telegram.org
* start date: Mar 24 13:48:17 2020 GMT
* expire date: May 23 16:17:38 2022 GMT
* subjectAltName: host "api.telegram.org" matched cert's "api.telegram.org"
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> POST /botmybottoken/sendPhoto HTTP/1.1
Host: api.telegram.org
Accept: */*
Content-Length: 254
Content-Type: multipart/form-data; boundary=------------------------9b6fc10336ea9470
* We are completely uploaded and fine
* old SSL session ID is stale, removing
< HTTP/1.1 400 Bad Request
< Server: nginx/1.16.1
< Date: Fri, 08 May 2020 09:25:10 GMT
< Content-Type: application/json
< Content-Length: 83
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
<
{"ok":false,"error_code":400,"description":"Bad Request: wrong HTTP URL specified"}* Connection #0 to host api.telegram.org left intact
* Closing connection 0
If I try to send this image buffer:
cv::Mat img(260, 301, CV_8UC3, cv::Scalar(0, 0, 0));
std::vector<uchar> buff;
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 80;
cv::imencode(".jpg", img, buff, param);
std::string strImg((char*)buff.data(), buff.size());
And if I modify the request code only from this:
curl_mime_data(part, "#D:/a.jpg", CURL_ZERO_TERMINATED);
To this:
curl_mime_data(part, strImg.data(), strImg.size());
I have this response in the console:
* Trying 149.154.167.220...
* TCP_NODELAY set
* Connected to api.telegram.org (149.154.167.220) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; CN=api.telegram.org
* start date: Mar 24 13:48:17 2020 GMT
* expire date: May 23 16:17:38 2022 GMT
* subjectAltName: host "api.telegram.org" matched cert's "api.telegram.org"
* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> POST /botmybottoken/sendPhoto HTTP/1.1
Host: api.telegram.org
Accept: */*
Content-Length: 2164
Content-Type: multipart/form-data; boundary=------------------------f5bde0e2e0ef8114
Expect: 100-continue
* old SSL session ID is stale, removing
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 400 Bad Request
< Server: nginx/1.16.1
< Date: Fri, 08 May 2020 09:30:34 GMT
< Content-Type: application/json
< Content-Length: 128
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
<
{"ok":false,"error_code":400,"description":"Bad Request: wrong remote file identifier specified: Wrong character in the string"}* Connection #0 to host api.telegram.org left intact
* Closing connection 0
I don't want to use any c++ telegram bot library because I have just to send a Message or a photo.
I' m able to send a message with cUrl code.
With photos, I have some problems.
Because I have to do this two simple actions I prefer to use only cUrl and nothing else.
I need help cause I'm not understanding what's wrong.
I know I write a lot but I wish this could help you to understand everything better and faster.
Thank you!
int sendTelegramPhoto(string chat_id, string path_to_photo, string caption = NULL){
CURL *curl;
CURLcode response;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_slist_append(headers, "Content-Type: multipart/form-data");
curl_slist_append(headers, "charset=utf-8");
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "chat_id");
curl_mime_data(part, chat_id.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "photo");
curl_mime_filedata(part, path_to_photo.c_str());
curl_mime_type(part, "image/jpeg");
part = curl_mime_addpart(mime);
curl_mime_name(part, "caption");
curl_mime_data(part, caption.c_str().c_str(), CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
response = curl_easy_perform(curl);
curl_mime_free(mime);
curl_slist_free_all(headers);
}
curl_easy_cleanup(curl);
return 0;
}
cv::Mat img(260, 301, CV_8UC3, cv::Scalar(0, 0, 0));
std::vector<uchar> buff;
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 80;
cv::imencode(".jpg", img, buff, param);
std::string strImg((char*)buff.data(), buff.size());
CURLcode ret;
CURL* hnd;
curl_mime* mime1;
curl_mimepart* part1;
mime1 = NULL;
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.telegram.org/bottoken/sendPhoto");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
mime1 = curl_mime_init(hnd);
part1 = curl_mime_addpart(mime1);
curl_mime_data(part1, "mychatid", CURL_ZERO_TERMINATED);
curl_mime_name(part1, "chat_id");
part1 = curl_mime_addpart(mime1);
curl_mime_data(part1, strImg.data(), strImg.size());
curl_mime_filename(part1, "myimage.jpg");
curl_mime_name(part1, "photo");
curl_easy_setopt(hnd, CURLOPT_MIMEPOST, mime1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.87.0-DEV");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
ret = curl_easy_perform(hnd);
...
I am using libcurl with C++ in order send a POST request off to query some information off of RateBeer on a particular beer. It says the data has been sent off but I am getting a "POST fields empty" error (which is from the graphQL side I assume).
The following is a dump of the current result (I have a print of the raw post data as well)
* Trying 13.249.142.63...
* TCP_NODELAY set
* Connected to api.r8.beer (13.249.142.63) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=*.r8.beer
* start date: Aug 14 00:00:00 2018 GMT
* expire date: Sep 14 12:00:00 2019 GMT
* subjectAltName: host "api.r8.beer" matched cert's "*.r8.beer"
* issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
* SSL certificate verify ok.
> POST /v1/api/graphql/ HTTP/1.1
Host: api.r8.beer
User-Agent: libcurl-agent/1.0
Content-Type:application/json
Accept:application/json
x-api-[key:Removed for privacy]
Transfer-Encoding: chunked
cb
* upload completely sent off: 210 out of 203 bytes
< HTTP/1.1 500 Internal Server Error
< Content-Type: application/json
< Content-Length: 61
< Connection: keep-alive
< Date: Thu, 31 Jan 2019 16:07:38 GMT
< x-amzn-RequestId: 547d7bc7-2572-11e9-b411-af48978c268e
< CF-RAY: 4a1d6eb288bda66b-DUB
< Access-Control-Allow-Origin: *
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-api-key, x-api-auth
< x-amzn-Remapped-Connection: keep-alive
< X-Request-Id: 54b37fa0-2572-11e9-ac7f-9d4fc71779d1
< Set-Cookie: __cfduid=d84de3db6937ad5bef73abca5ae68e43a1548950858; expires=Fri, 31-Jan-20 16:07:38 GMT; path=/; domain=.r8.beer; HttpOnly; Secure
< x-amz-apigw-id: UYGDpFkeDoEFR2w=
< x-amzn-Remapped-Server: cloudflare
< X-Powered-By: Express
< x-amzn-Remapped-Date: Thu, 31 Jan 2019 16:07:38 GMT
< X-Cache: Error from cloudfront
< Via: 1.1 feeead777aa6b11f4775062f1953fdc4.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: kg0xraCi9C9zdmNff3iEZXOBpTMKPwNSQa6nzV-tyXqCdVCFFOP2Jg==
<
* Connection #0 to host api.r8.beer left intact
{"query":"query{\nTruth:beerSearch(query: \"Truth\", first : 5) {\n...beerfields\n}\n}\nfragment beerfields on BeerList{\nitems{\nname\nbrewer{\nname\n}\ndescription\nratingCount\n}\n}","variables":"{}"}
Response: POST body missing. Did you forget use body-parser middleware Response End
Some debugging I have tried in this conquest:
Changing the code to use a readback callback instead of using the POST_FIELDS default, same result
Messing with the capitalization of headers
Changed the transfer method to using chunked
Trying to POST to the sandbox API and the production API
Here is the query function:
NOTE: m_api is just a member with the api key and the api base URL
void cURLWrapperRateBeer::QueryBeerList(const CBeerList& inBeerList)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl)
{
// Construct the header struct
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "Accept:application/json");
std::string keyPhrase = std::string("x-api-key:").append(m_api.second);
headers = curl_slist_append(headers, keyPhrase.c_str());
headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
// Set the URL and the data we want
curl_easy_setopt(curl, CURLOPT_URL, m_api.first.c_str());
// Signify our intention to post data
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// Verbose Output
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Give it a user agent
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
std::string data = CreateRequestURL(inBeerList);
data.append("\0");
// Set up the data container for our push request
const char* c_data = data.c_str();
std::cout << c_data;
struct WriteStatus data_container;
data_container.p = c_data;
data_container.remaining = (long)strlen(c_data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_container.p);
/*
Define the buffer for writing memory and what the callback function will
use for its buffer
*/
//curl_easy_setopt(curl, CURLOPT_READFUNCTION, &cURLWrapperRateBeer::ReadData);
//curl_easy_setopt(curl, CURLOPT_READDATA, &data_container);
// Size of the data
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data_container.remaining);
// Handler for reading response
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &cURLWrapperRateBeer::WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Set our custom set of headers
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
//std::cout << "DUMP : " << data_container.p;
}
#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
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
std::cout << "Response: " << response;
std:: cout << "Response End";
/* Check for errors */
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
I use libcurl to do RTSP request. I set curl options as seen below:
FILE *tmpListDownloadFile;
tmpListDownloadFile = fopen(tmp.c_str(), "w");
if(tmpListDownloadFile != NULL)
{
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
char *p = curl_easy_unescape(curl, UriP, 0, NULL);
string s = p;
curl_free(p);
string uri = url + "/?" + s;
printf("%s uri:%s\n",__FUNCTION__,uri.c_str());
curl_version_info_data *d = curl_version_info(CURLVERSION_NOW);
printf("curl version:%s\n",d->version);
curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri.c_str());
curl_easy_setopt(curl, CURLOPT_RTSP_CLIENT_CSEQ, stRtspInfo.CSeq);
curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport.c_str());
curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)tmpListDownloadFile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
CURLcode curlResult = curl_easy_perform(curl);
char* session_id;
if(curl_easy_getinfo(curl, CURLINFO_RTSP_SESSION_ID, &session_id) == CURLE_OK)
{
printf("%s session_id:%s\n",__FUNCTION__, session_id);
if(NULL != session_id)
{
ostringstream ss;
ss << session_id;
stRtspInfo.sessionID = ss.str();
bIsSessionEstablished = true;
}
}
else
{
printf("%s getting session id failed\n",__FUNCTION__);
}
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(tmpListDownloadFile);
if (curlResult == CURLE_OK && bIsSessionEstablished == true)
{
...
}
else
{
printf("Setup failed\n");
}
}
And log messages are as below.
RtspSetup url:rtsp://10.134.158.71
RtspSetup transport:RTP/AVP;unicast;client_port=45636-45637
RtspSetup uri:rtsp://10.134.158.71/?src=1&freq=11054&sr=30000&pol=v&msys=dvbs2&mtype=8psk&fec=34&ro=0.35&plts=off&pids=0
curl version:7.21.3
* About to connect() to 10.134.158.71 port 554 (#0)
* Trying 10.134.158.71... * connected
* Connected to 10.134.158.71 (10.134.158.71) port 554 (#0)
> SETUP rtsp://10.134.158.71/?src=1&freq=11054&sr=30000&pol=v&msys=dvbs2&mtype=8psk&fec=34&ro=0.35&plts=off&pids=0 RTSP/1.0
CSeq: 1
Transport: RTP/AVP;unicast;client_port=45636-45637
If-Modified-Since: Thu, 01 Jan 1970 00:00:00 GMT
* HTTP 1.1 or later with persistent connection, pipelining supported
< RTSP/1.0 200 OK
< CSeq: 1
< Date: Sat, Jan 01 2000 00:14:11 GMT
< Transport: RTP/AVP;unicast;destination=10.134.158.14;source=10.134.158.71;client_port=45636-45637;server_port=6970-6971
< Session: E3C33231;timeout=60
< com.ses.streamID: 3
<
* Connection #0 to host 10.134.158.71 left intact
RtspSetup session_id:E3C33231
Altough I set Timecondition as none, if-modified-since are added to the header. I want to remove if-modified-since header. How to do this?
That's a bug in libcurl (that happens for some RTSP requests). Just now fixed in this commit. To be included in the upcoming next release: 7.46.0.
how it can be that in windows visual studio 2013 curl_easy_perform returns cannot_resolve host but same code in same pc works fine in ubuntu virtual machine ?
CURL *curl;
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, "https://myhost.com/api/login/");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
// calculate request header
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"username\":\"name\", \"password\":\"test\"}" );
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); // set callback
curl_easy_perform(curl); // perform request
curl_easy_cleanup(curl); // cleanup request
return 0;
the verbose output of programm is following
> * STATE: INIT => CONNECT handle 0x907ef0; line 1034 (connection #-5000)
> * Added connection 0. The cache now contains 1 members
> * STATE: CONNECT => WAITRESOLVE handle 0x907ef0; line 1071 (connection #0)
> * Could not resolve host: myhost.com
> * Closing connection 0
> * The cache now contains 0 members
> * Expire cleared
so i find out the solution.
the problem was in curl_global_init(CURL_GLOBAL_SSL); function call.
when i change it to curl_global_init(CURL_GLOBAL_ALL); the program starts work as expected.