简体   繁体   English

将自定义数据添加到 C++ 中的 cURL 请求

[英]Add custom data to cURL request in C++

I'm working on a project that needs a licensing system.我正在做一个需要许可系统的项目。 So I decided to use the licensing system of my sales platform Gumroad.所以我决定使用我的销售平台 Gumroad 的许可系统。

It seems pretty straightforward, the only thing is that I can't seem to add custom data to a cURL request in C++.看起来很简单,唯一的事情是我似乎无法将自定义数据添加到 C++ 中的 cURL 请求中。

This is the example posted in the documentation:这是文档中发布的示例:

curl https://api.gumroad.com/v2/licenses/verify \
  -d "product_permalink=QMGY" \
  -d "license_key=YOUR_CUSTOMERS_LICENSE_KEY" \
  -X POST

This works very well from the Ubuntu Terminal, but when I try to implement it in C++, it doesn't work.这在 Ubuntu 终端上效果很好,但是当我尝试在 C++ 中实现它时,它不起作用。

This is the C++ code:这是 C++ 代码:

std::stringstream get_response()
{
   std::stringstream str;
   curl::curl_ios<std::stringstream> writer(str);

   curl::curl_easy easy(writer);

   easy.add<CURLOPT_URL>("https://api.gumroad.com/v2/licenses/verify");
   easy.add<CURLOPT_POSTFIELDS>("product_permalink=QMGY");
   easy.add<CURLOPT_POSTFIELDS>("license_key=YOUR_CUSTOMERS_LICENSE_KEY");

   try
   {
      easy.perform();
   }
   catch (curl::curl_easy_exception error)
   {
      auto errors = error.get_traceback();
      error.print_traceback();
   }

   return str;
}

It is able to get a response from the server, but it doesn't recognize the license key.它能够从服务器获得响应,但它无法识别许可证密钥。

I assume that this is because CURLOPT_POSTFIELDS is not the right field to replace -d .我认为这是因为CURLOPT_POSTFIELDS不是替换-d的正确字段。

Does anyone know the right field to use?有谁知道使用正确的领域?

There are like 300 different ones, and I have no clue which one is the right one.大约有 300 种不同的,我不知道哪一种是正确的。

Can someone please point me in the right direction?有人可以指出我正确的方向吗?

pretty sure you forgot easy.add<CURLOPT_POST>(1);很确定你忘记了easy.add<CURLOPT_POST>(1);

Per the curl documentation for the -d option:根据-d选项的curl 文档

-d, --data -d,--数据

(HTTP MQTT) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. (HTTP MQTT) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的一样。 This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.这将导致 curl 使用 content-type application/x-www-form-urlencoded 将数据传递到服务器。 Compare to -F, --form.与 -F、--form 进行比较。

--data-raw is almost the same but does not have a special interpretation of the @ character. --data-raw 几乎相同,但对 @ 字符没有特殊解释。 To post data purely binary, you should instead use the --data-binary option.要发布纯二进制数据,您应该改用 --data-binary 选项。 To URL-encode the value of a form field you may use --data-urlencode.要对表单字段的值进行 URL 编码,您可以使用 --data-urlencode。

If any of these options is used more than once on the same command line, the data pieces specified will be merged with a separating &-symbol.如果在同一命令行上多次使用这些选项中的任何一个,则指定的数据片段将与分隔符 &-symbol 合并。 Thus, using '-d name=daniel -d skill=lousy' would generate a post chunk that looks like 'name=daniel&skill=lousy'.因此,使用'-d name=daniel -d Skill=lousy'会生成一个看起来像'name=daniel&skill=lousy'的帖子块。

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin.如果您以字母 @ 开头数据,则 rest 应该是从中读取数据的文件名,或者 - 如果您希望 curl 从标准输入读取数据。 Posting data from a file named 'foobar' would thus be done with -d, --data @foobar.因此,将使用 -d, --data @foobar 从名为“foobar”的文件发布数据。 When -d, --data is told to read from a file like that, carriage returns and newlines will be stripped out.当 -d, --data 被告知从这样的文件中读取时,将删除回车符和换行符。 If you do not want the @ character to have a special interpretation use --data-raw instead.如果您不希望 @ 字符具有特殊解释,请改用 --data-raw。

Examples:例子:

curl -d "name=curl" https://example.com curl -d "name=curl" https://example.com
curl -d "name=curl" -d "tool=cmdline" https://example.com curl -d "name=curl" -d "tool=cmdline" https://example.com
curl -d @filename https://example.com curl -d @filename https://example.com

See also --data-binary, --data-urlencode and --data-raw.另请参见 --data-binary、--data-urlencode 和 --data-raw。 This option overrides -F, --form and -I, --head and -T, --upload-file.此选项覆盖 -F、--form 和 -I、--head 和 -T、--upload-file。

Note the part about specifying -d multiple times.请注意有关多次指定-d的部分。

CURLOPT_POSTFIELDS is the correct field to use. CURLOPT_POSTFIELDS是要使用的正确字段。 You are simply using it the wrong way.您只是以错误的方式使用它。 You need to set CURLOPT_POSTFIELDS only once, providing it with all of the data to be posted, in the correct format the server is expecting.您只需要设置一次CURLOPT_POSTFIELDS ,以服务器期望的正确格式为它提供所有要发布的数据。 Since you are trying to post a application/x-www-form-urlencoded format, the data needs to be name=value pairs separated by & , eg:由于您尝试发布application/x-www-form-urlencoded格式,因此数据需要是由&分隔的name=value对,例如:

std::stringstream get_response()
{
   std::stringstream str;
   curl::curl_ios<std::stringstream> writer(str);

   curl::curl_easy easy(writer);

   easy.add<CURLOPT_URL>("https://api.gumroad.com/v2/licenses/verify");
   easy.add<CURLOPT_POSTFIELDS>("product_permalink=QMGY&license_key=YOUR_CUSTOMERS_LICENSE_KEY");

   try
   {
      easy.perform();
   }
   catch (curl::curl_easy_exception error)
   {
      auto errors = error.get_traceback();
      error.print_traceback();
   }

   return str;
}

If any of the name or value data may contain reserved or non-ASCII characters, you will have to url-encode that data, such as with curl_easy_escape() .如果任何namevalue数据可能包含保留字符或非 ASCII 字符,则必须对该数据进行 url 编码,例如使用curl_easy_escape()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM