I am tried this but it is not working let me know what is wrong with the following code:
static const char *postthis="[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown cc\", \"subject\":\"Life is subject\"}]";
static const char *postthis1= "http://192.168.0.164:8983/solr/collection1/update?wt=json&commit=true ";
static const char *postthis2= "'Content-type: application/json'";
struct curl_slist *list = NULL;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,postthis1 );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
list = curl_slist_append(list, "-H");
list = curl_slist_append(list, postthis2);
list = curl_slist_append(list, "-d");
/* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
itself */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK){
logger.LogError("curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
with the above code I am getting response on terminal window as :
{"responseHeader":{"status":0,"QTime":9}}
which is response of above request.
and in solr log I am getting following things:
INFO - 2016-05-19 11:22:10.778; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; start commit{,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false}
INFO - 2016-05-19 11:22:10.779; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; No uncommitted changes. Skipping IW.commit.
INFO - 2016-05-19 11:22:10.781; [ x:collection1] org.apache.solr.core.SolrCore; SolrIndexSearcher has not changed - not re-opening: org.apache.solr.search.SolrIndexSearcher
INFO - 2016-05-19 11:22:10.783; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; end_commit_flush
INFO - 2016-05-19 11:22:10.784; [ x:collection1] org.apache.solr.update.processor.LogUpdateProcessor; [collection1] webapp=/solr path=/update params={commit=true&[{"id":"4001", "from":"Life is to", "to":"unknown cc", "subject":"Life is subject"}]=&wt=json} {commit=} 0 8
Actual curl command is :
curl http://192.168.0.164:8983/solr/collection1/update?commit=true -H "Content-Type: application/json" -d '[{"id":"450", "to":"Life is .", "cc":"unknown", "subject":"Life"}]'
it is working fine for indexing.
Related
i am new to cUrl.
What i need todo is a curl request to an ldap server.
So far the request works fine over the command line:
$curl ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
After trying to get this done in my C++ CMake Project. I issued this error Code from
libcurl: libcurl: (39) LDAP local: ldap_search_ext Bad search filter
My code as snippet:
std::string readBuffer;
CURL* curl = curl_easy_init();
if (curl) {
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
errbuf[0] = 0;
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // this line prints into console, what curl is trying to do
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L); // closes the connection, if not here the connection can be used more than once
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
std::cout << res << std::endl;
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
if (res != CURLE_OK) {
size_t len = strlen(errbuf);
fprintf(stderr, "\nlibcurl: (%d) ", res);
if (len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len - 1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(res));
}
}
My Output:
Test curl library
* Trying XXXXXXXXXXXXXX
* TCP_NODELAY set
* Connected to XXXXXXXXXXXXXX
* LDAP local: ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
* LDAP local: ldap_search_ext Bad search filter
* Closing connection 0
39
libcurl: (39) LDAP local: ldap_search_ext Bad search filter
I appricate every help! Thank you for taking the time reading this.
You cannot just bung everything into the CURLOPT_OPT_URL field. The --user bit should be separated into a separate parameter:
curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX)");
curl_easy_setopt(curl, CURLOPT_USERPWD, "s_ad_XXXXXXXXX:Password");
I want to create a shared link for a dropbox file using curl & C++ on a windows 10 desktop.
I've already manage to upload the file to a dropbox folder using curl & C++.
When I try to create the link with command line it works with
curl -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings --header "Authorization: Bearer <TOKEN>" --header "Content-Type: application/json" --data "{\"path\":\"path_of_the_file\"}"
but when I use this code to do the same in C++ it hangs at < HTTP/1.1 100 Continue
Here is my code :
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
string readBuffer;
printf("Running curl test get shared link.\n");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); //no ssl
struct curl_slist *headers = NULL; // init to NULL is important
headers = curl_slist_append(headers, "Authorization: Bearer <TOKEN>");
headers = curl_slist_append(headers, "Content-Type: ");
headers = curl_slist_append(headers, "Dropbox-API-Arg: {\"path\":\"path_of_file\"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings");
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// 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);
cout << readBuffer << endl;
printf("\nFinished curl test.\n");
}
curl_global_cleanup();
printf("Done get shared link!\n");
I've tried with content-type : application/json and adding fields but I can't reproduce what I'm doing with the command line
There are errors in the code, causing undefined behaviour.
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
cUrl has a C API, C does not have overloads. Thus curl_easy_setopt is a multi-arg function, and the third argument type depends on the second argument value. CURLOPT_SSL_VERIFYPEER and CURLOPT_VERBOSE require long values, you pass the values as int. The size of the third argument is important exactly like this is in *printf functions. The proper calls must be
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
The second. 100 Continue means you have not passed the data for the POST request. The server received request and waits for further data. See
libcurl example - http-post.c.
I can't reproduce what I'm doing with the command line
Add the --libcurl to the command line for getting a C code performing the same actions as in the command line.
Thank you S.M. Here is the code that works
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
string readBuffer;
printf("Running curl test get shared link.\n");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //no ssl
struct curl_slist *headers = NULL; // init to NULL is important
headers = curl_slist_append(headers, "Authorization: Bearer <TOKEN>");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"path\":\"path_to_the_file"}");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings");
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// 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);
cout << readBuffer << endl;
printf("\nFinished curl test.\n");
}
curl_global_cleanup();
printf("Done get shared link!\n");
I am trying to replicate the following curl command in c++ code using the curl library but with no luck.
The curl command is (the url is an actual url I am just hiding it):
curl -iX PATCH '*URL*/attrs/topicData' \
-H 'Content-Type: application/json' \
-H 'Link: <http://context-provider:3000/data-models/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
--data-raw '{
"value": "Hi, new data test",
"type": "Property"
}'
This works perfectly fine and updates the value as required. My issues is that I can't replicate it in c++ code. I am using the nlohmann json library just in case that helps.
My c++ code is:
json ent={
{"type","Property"},
{"value","updated successfully"}
};
curl_global_init(CURL_GLOBAL_DEFAULT);
std::string json_entity = ent.dump();
curl = curl_easy_init();
if (curl) {
// Add headers
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, R"(Link: <http://context-provider:3000/data-models/ngsi-context.jsonld>; rel="http://w3.org/ns/json-ld#context"; type="application/ld+json")");
// Set custom headers
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set URL
curl_easy_setopt(curl, CURLOPT_URL, "*URL*/attrs/topicData");
// Set request type
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
// Set values
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,json_entity);
// Perform the request which prints to stdout
result = curl_easy_perform(curl);
// Error check
if (result != CURLE_OK) {
std::cerr << "Error during curl request: "
<< curl_easy_strerror(result) << std::endl;
}
//Free header list
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
else {
std::cerr << "Error initializing curl." << std::endl;
}
The error that I am getting is:
"type":"http://uri.etsi.org/ngsi-ld/errors/InvalidRequest",
"title":"Invalid request.",
"details":"Invalid request."
I think my issue is at set values command but I am not sure what the problem is.
Can anyone please advice me on what I am doing wrong?
CURLOPT_POSTFIELDS expects a char* but you are supplying a std::string.
This should be working better:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_entity.data());
i want to index data using curl command in c++
the curl command i am running through linux is getting updated in solr but not via c++ code
curl command is
curl http://192.168.0.164:8983/solr/collection2/update?commit=true -H 'Content-type: application/json' -d '[{"id":"4456", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'
and c++ code i m trying to execute is not updating the solr
CURL *curl = curl_easy_init();
CURLcode res;
static const char *postthis="'[{\"id\":\"4123\",\"from\":\"tarana\", \"to\":\"anuja\", \"cc\":\"unknown cc\",\"bcc\":\"unknown bcc\", \"src_ip\":\"192.168.0.156\",\"dst_ip\":\"192.168.0.40\",\"dst_port\":\"5454\",\"dst_port\":\"4545\",\"subject\":\"abc def ghi jhkl\",\"content\":\"this is testing\",\"interfaceid\":\"1\",\"locationid\":\"1\",\"date_time\":\"2014-12-31T01:59:59Z\"}]'";
static const char *postthis1= "http://192.168.0.164:8983/solr/collection2/update?commit=true ";
static const char *postthis2= "'Content-type: application/json'";
struct curl_slist *list = NULL;
logger.LogError("postthis %s",postthis);
logger.LogError("postthis1 %s",postthis1);
logger.LogError("postthis2 %s",postthis2);
//curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,postthis1 );
list = curl_slist_append(list, "-H");
list = curl_slist_append(list, postthis2);
list = curl_slist_append(list, "-d");
//if we don't provide POSTFIELDSIZE, libcurl will strlen() by itself
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
//Check for errors
if(res != CURLE_OK){
logger.LogError("curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}else if(res == CURLE_OK){
logger.LogError("res is CURLE_OK");
}
//always cleanup
curl_easy_cleanup(curl);
If I do
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);
the server will do not see multipart, but if I coment the second line
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);
it'll do.
What's wrong here?
UPD: So, now the question is, whether it's possible to put value to a param, so that that value was in POST parameters array on server-side? I'm trying
headers = curl_slist_append(headers, "Content-Disposition: form-data");
//action=upload
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "action",
CURLFORM_COPYCONTENTS, "upload",
CURLFORM_CONTENTHEADER, headers,
CURLFORM_CONTENTTYPE, "Content-Type: multipart/form-data",
CURLFORM_END);
But that's not working!
Thank you in advance!
I ended up mostly following the example code here: http://curl.haxx.se/libcurl/c/multi-post.html
Here's a sample of the code that ended up working for me based on the curl example link:
...
CURL *curl;
curl_mime *form;
curl = curl_easy_init();
/* Set the headers for the request. */
scoped_curl_slist headers;
headers.list = NULL;
string session_header = "Authorization: Bearer " + token;
headers.list = curl_slist_append(headers.list, session_header.c_str());
headers.list = curl_slist_append(headers.list, "cache-control: no-cache");
headers.list = curl_slist_append(headers.list, "Content-Type: multipart/form-data");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.list);
curl_easy_setopt(curl, CURLOPT_URL, "your_url.com");
/* Create the form */
form = curl_mime_init(curl);
/* Fill in the file upload field */
curl_mimepart *field = curl_mime_addpart(form);
curl_mime_name(field, "upload");
curl_mime_filedata(field, "C:/file_name.txt");
/* Fill in the filename field */
field = curl_mime_addpart(form);
curl_mime_name(field, "name");
curl_mime_data(field, "file_name.txt", CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
CURLcode result = curl_easy_perform(curl);
...
curl_mime_free(form);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);