【发布时间】:2014-05-27 23:43:18
【问题描述】:
我在 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++ 程序执行它。我之前已经设置并使用了 cURL 来从 ftp 示例进行 ftp 上传和下载。但是我没有找到任何例子来做到这一点。
我想知道如何将用户名和密码参数传递给 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),以便对我必须做的事情一无所知。
我是一名图形程序员 :) 不太擅长网络服务。 如有任何帮助,我将不胜感激。
【问题讨论】: