如何解决cURL发送POST数据二进制
我有cURL命令:
curl "https://pelevin.gpt.dobro.ai/generate/" --data-binary '{"prompt" : "text","length" : 40}
我想在C ++中做同样的事情。
我尝试过:
string params = "prompt=text&length=40";
string buffer;
CURL* curl;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl,CURLOPT_URL,"https://pelevin.gpt.dobro.ai/generate/");
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_callback);
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,params.size());
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,params.c_str());
curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
并收到如下消息:
{"detail":[{"loc":["body",0],"msg":"Expecting value: line 1 column 1 (char 0)","type":"value_error.jsondecode","ctx":{"msg":"Expecting value","doc":"prompt=text&length=40","pos":0,"lineno":1,"colno":1}}]}
如何正确提出请求?
解决方法
当您从命令行转到libcurl时,您更改了提供的数据。为什么?
首先,它是一个JSON对象字符串(这似乎是服务器所期望的)。现在,您已经将其设置为URL编码的字符串。
只需将正确的数据传递到服务器,就像在第一个示例中一样。
string params = "{\"prompt\" : \"text\",\"length\" : 40}";
如果您愿意,可以使用一些“更好”的方式在现代C ++中进行格式化:
string params = R"RAW({"prompt" : "text","length" : 40})RAW";
顺便说一句,对命令行中指定的数据使用--data-binary
很奇怪。 It's intended for use with file input,因为它可以防止从文件中删除换行符。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。