can't figure out the libcurl C/C++ code equivalent of a curl command - c++

I'm currently working on a visual studio C++ project that needs to use libcurl to access some apis of my website which uses Django.
I'm using django-tastypie to provide api, and right now I can use this api by using this curl command:
curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"body": "This actually is my last post."}' http://localhost:8000/api/v1/entry/4/
to update data on my server. As a test, it works, but what I really need is to write code in C/C++ with libcurl that does the same.
I'm just starting to look at libcurl and not quite sure how to do that. I assume that I should use curl_easy_setopt but I can't find the right options to set.
So my question is
Is it possible to use libcurl to achieve the same goal as that command? I'm not familiar with libcurl.
If so, how to set options like -x PATCH and content-Type? I looked at the document and didn't seem to find how.
Thanks for any help.

As #deltheil suggested, I tried --libcurl.
But notice the code generated isn't quite right in my case. The -H "Content-Type: application/json" part wasn't in the code.
You need to set headers like this
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
But the option --libcurl is quite helpful.
This post about json requests in C helped me a lot.

Related

libcurl POST xml file to url

I have been working on uploading a simple XML file to a specialized Web Server. This webserver only supports the use of POST or GET, so it eliminates the use of CURLOPT_UPLOAD which only provides for PUT.
I can use the command line to send the file using the following command line
curl -H 'Content-Type: text/xml; charset=utf-8' -H 'SOAP Action:'
-T igs_meters2.xml -X POST //url//
I tried to use CURLOPT_POST and CURLOPT_READDATA, but received a 413 Request Entity Too Large.
I know this cannot be this hard, but I am out in areas I have never work in. Thanks in advance for an idea.
SOLVED:
the tip about CUSTOMREQUEST was what I needed to find. That helped, and I also needed to add in an HTTPHEADER for CONTENT-TYPE and SOAPAction:

Qt : Accessing Secure Objects using session-id from a server

I am working on a Qt application in which I would like to retrieve objects which are tagged #Secured in the Spring-MVC based web-application. I have written the code on the server side for REST authentication and if I use the 'curl' tool, then I am able to access secure services on the server. I would like to know if there is anyway I can replicate the way I am accessing secured services in a QT application. Any help would be nice. Thanks a lot.
curl code :
curl -i -X POST -d j_username=email#email.de -d j_password=password -c /home/username/cookies.txt http://localhost:8080/j_spring_security_check
And for using the session-id to access secure services, I do :
curl -i --header "Accept:application/json" -X GET -b /home/cookies.txt http://localhost:8080/secure_service_method
How can I achieve the same thing in QT. Any help would be nice. Please note, I am not that of an expert in Qt, so please be patient. Thanks a lot.
Converting my comments into this answer:
Judging from your curl command you probably just need to send a POST request and handle the cookies using Qt . Qt provides everything necessary to accomplish this task, have a look at QNetworkAccessManager::post and QNetworkAccessManager::setCookieJar.
The basic steps you need to implement will look like this:
1.) Setup:
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
manager->setCookieJar(new QNetworkCookieJar(manager));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
2.) Request the cookie
QNetworkRequest request;
request.setUrl(QUrl("http://localhost:8080/j_spring_security_check"));
QByteArray postData;
postData.append("j_username=email#email.de&j_password=password");
manager->post(request,postData);
3.) Use the manager object which then has the cookie to request the protected files.

Setting --data-binary using libcurl in c++

I'm using the curl api from http://curl.haxx.se/ in a c++ application. I'm trying to write the equivalent of:
curl -v -X POST -H "Content-type: application/json; charset=utf-16" --data-binary #some data.json
I'm using curl_easy_setopt() to set up the various options but the bit I can't figure out is the --data-binary flag. Which CURLOPT setting would I use to set this?
Thanks in advance
You should use CURLOPT_POSTFIELDS:
specify data to POST to server [...] Pass a char * as parameter, pointing to the full data to send in a HTTP POST operation. You must make sure that the data is formatted the way you want the server to receive it.
Note: you can easily discover this by using the --libcurl option:
--libcurl FILE Dump libcurl equivalent code of this command line

converting curl api call into restful api call

i am very new to the java EE.
I have built some simple restful client for calling rest api's from java using jersey framework.
I have one curl request which i have to call from java
curl -i -X POST 'https://{url}' -H 'Content-Type: application/x-www-form-urlencoded' -H "Accept: application/vnd.newbay.dv-1.8+json" -H "X-HTTP-METHOD-OVERRIDE: DELETE" -H "X-Client-Platform: SERVICES" -H "X-Client-Identifier: IL" --insecure --data 'path=%2FMy+Samsung+GT-I9082%2F20130822_172409.jpg&path=%2FMy+Samsung+GT-I9082%2F20130905_085407.jpg'
Here the paths of file are passed as form post in curl using --data.
Can anyone tell me how to call this api from my java program using jersey framework? I am having difficulty because of this --data part? how exactly i will pass that in my restful java client that i cant understand.
Also can anyone tell me how to pass form post --data part while calling the same api from soapUI?
In your cURL command, --data provides post data in a classic html form format. You shoud alrealy be aware of that because the content-type is explicit : application/x-www-form-urlencoded
Jersey can handle this encoding for you, using MultivaluedMapImpl.
see Sending Name Value Pair in POST using Jersey Client

How do I use the --header option to send cookies with Siege?

I have just started to use Siege to do load/stressing test on a new web server. I am trying to test on my most resource/performance heavy script, but the script requires cookies. What is the proper format for using the --header option in siege?
I have tried this with no luck:
siege --header="Set-Cookie: PHPSESSID=--COOKIE--; iptoken=--COOKIE--" http://www.myurl.com/script.php,
There is no documentation on this that I could find, so any ideas/suggestions would be appreciated.
The answer is to use --header="Cookie: --COOKIE_DATA--" (ref. wiki.wsmoak.net/cgi-bin/wiki.pl?Siege).