I am using the c++ libcurl to send a POST request to a webpage, but i am struggling test it. The code is use is:
#include <stdio.h>
#include <curl/curl.h>
#include <string>
using namespace std;
int main(void)
{
CURL *curl = curl_easy_init();
if(curl) {
const char *data = "submit = 1";
curl_easy_setopt(curl, CURLOPT_URL, "http://10.5.10.200/website/WebFrontend/backend/posttest.php");
/* size of the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 10L);
/* pass in a pointer to the data - libcurl will not copy */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_perform(curl);
}
/* Perform the request, res will get the return code */
/* always cleanup */
return 0;
}
This is the sample code from: https://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html
The outcome really confuses me. From the terminal I can see there is POST request been sent but from the web page i cannot retrieve any data. The web page is very simple php code that prints out the $_POST. terminal screenshot and webpage screenshot
Could anyone help me with this? Why i cannot get the POST request from the web page, and how can i fix this? Anyone can give me a better way to test the code? Thank you guys so much!
To make a POST request with Curl, you can run the Curl command-line tool with the -d or --data command-line option and pass the data as the second argument. Curl will automatically select the HTTP POST method and application/x-www-form-urlencoded content type for the transmitted data.
libcurl is a library of functions that are provided with a C API, for applications written in C. You can easily use it from C++ too, with only a few considerations (see libcurl for C++ programmers).
libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!
Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .
You have to implement a callback function that will be called by curl at every batch of data received.
See a good example here :
https://gist.github.com/alghanmi/c5d7b761b2c9ab199157#file-curl_example-cpp
Obviously you replace the simple string by whatever data type and treatment you need in the WriteCallback() function.
Copy/paste of alghanmi's example :
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
Also, you'll find a good tutorial here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With