我有以下的cURL命令,在终端中能够正常工作。
curl --insecure -X POST --data "username=testuser&password=12345" https://m360-prototype.herokuapp.com/sessions.json
这个json api发送了一些参数,例如-- "status":{"code":200,"message":"OK"}
现在我想让我的c++程序执行它。我之前已经为ftp上传和下载设置和使用过cURL,但是我没有找到任何例子来实现这个。
我想知道如何将用户名和密码参数传递给json api,并从中获取响应。
这是我在网上找到的代码,但它没有起作用。
struct curl_slist *headers=NULL; // init to NULL is important
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://m360-prototype.herokuapp.com/sessions.json");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=testuser&password=12345");
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *ct;
/* ask for the content-type */
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
printf("We received Content-Type: %s\n", ct);
}
}
如何从网络获得响应?我知道它将以字符串形式呈现,并且我足够能力来解析它。
我正在查找在终端上执行的curl命令传递的所有参数(--insecure,-X,POST,--data),以便对我要做的事情有一点了解。
我是一名图形程序员:)不太擅长Web服务。 我会非常感谢任何帮助。