如何将可变数据传递到 libcurl post 正文中?
How do I pass variable data into libcurl post body?
我在使用 libcurl 时将变量值传递到 POST 正文时遇到问题。这是我的代码:
#include "curl/curl.h"
char *fullname ="morpheus";
char *role = "leader";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\"name\": \"${fullname}\" ,\"job\":\"${role}\"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
您将变量本身的名称作为字符串传递。您必须连接值,而不是名称。
#include "curl/curl.h"
char *fullname ="morpheus";
char *role = "leader";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
char *data = "{\"name\":";
strcat(data, fullname);
strcat(data, ",\"job\":\");
strcat(data, role);;
strcat(data, "\"}");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
这当然不是最干净的方法,但它会奏效。
我在使用 libcurl 时将变量值传递到 POST 正文时遇到问题。这是我的代码:
#include "curl/curl.h"
char *fullname ="morpheus";
char *role = "leader";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\"name\": \"${fullname}\" ,\"job\":\"${role}\"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
您将变量本身的名称作为字符串传递。您必须连接值,而不是名称。
#include "curl/curl.h"
char *fullname ="morpheus";
char *role = "leader";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
char *data = "{\"name\":";
strcat(data, fullname);
strcat(data, ",\"job\":\");
strcat(data, role);;
strcat(data, "\"}");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
这当然不是最干净的方法,但它会奏效。