我一直用c ++获得http / 1.0 400 json rpc错误

时间:2017-09-24 23:23:49

标签: c++ json curl

#include <stdio.h>
#include <iostream>
#include <thread>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <array>
#include <curl/curl.h>

size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(int argc, char * argv[])
{
curl_global_init(CURL_GLOBAL_ALL);

CURL* easyhandle = curl_easy_init();
std::string readBuffer;

curl_easy_setopt(easyhandle, CURLOPT_URL, "http://192.168.1.79:8080");
curl_easy_setopt(easyhandle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easyhandle, CURLOPT_POST, 1);
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS,"{ \"jsonrpc\": \"2.0\", \"method\": \"get\", \"params\": [\"Frozen\"], \"id\": 3}");
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &readBuffer);

curl_easy_perform(easyhandle);

std::cout << readBuffer << std::endl;
//std::cout << "{ \"jsonrpc\": \"2.0\", \"method\": \"getTitles\", \"params\": [clea], \"id\": 3}" << std::endl;

return 0;
}

这是我的curl命令的代码。我看到请求在我的服务器上通过,但它一直说rpc错误。我认为这是我格式化我的json的方式。基本上在我的电脑上,如果我在终端运行这个curl命令,它的工作原理。但是,在c ++中运行它是行不通的。

1 个答案:

答案 0 :(得分:0)

原来“冻结”不在图书馆里。在服务器上添加它之后就可以了。