curl PATCH 更新值作为 curl 命令工作,但在 libcurl c++ 中不工作,任何想法有什么问题?
curl PATCH to update value works as a curl command but not in libcurl c++, any ideas what is wrong?
我正在尝试使用 curl 库在 C++ 代码中复制以下 curl 命令,但没有成功。
curl 命令是(url 是一个实际的 url 我只是隐藏它):
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"
}'
这工作得很好,并根据需要更新值。我的问题是我无法在 C++ 代码中复制它。我正在使用 nlohmann json 库以防万一。
我的 C++ 代码是:
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;
}
我得到的错误是:
“类型”:“http://uri.etsi.org/ngsi-ld/errors/InvalidRequest”,
"title":"无效请求。",
“详细信息”:“无效请求。”
我认为我的问题出在设置值命令上,但我不确定是什么问题。
谁能告诉我我做错了什么?
CURLOPT_POSTFIELDS
期望 char*
但您提供的是 std::string
.
这应该会更好:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_entity.data());
我正在尝试使用 curl 库在 C++ 代码中复制以下 curl 命令,但没有成功。 curl 命令是(url 是一个实际的 url 我只是隐藏它):
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"
}'
这工作得很好,并根据需要更新值。我的问题是我无法在 C++ 代码中复制它。我正在使用 nlohmann json 库以防万一。
我的 C++ 代码是:
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;
}
我得到的错误是: “类型”:“http://uri.etsi.org/ngsi-ld/errors/InvalidRequest”, "title":"无效请求。", “详细信息”:“无效请求。”
我认为我的问题出在设置值命令上,但我不确定是什么问题。
谁能告诉我我做错了什么?
CURLOPT_POSTFIELDS
期望 char*
但您提供的是 std::string
.
这应该会更好:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_entity.data());