【发布时间】:2014-04-12 18:21:28
【问题描述】:
我正在尝试将 JSON 数据从 bash 发布到 url,使用:
$ curl -v -d '{xxx:200}&apikey=xxxxx' -X POST http://localhost/xxxx/input/post.json -H "Accept: application/json" -H "Content-Type:application/json"
在 C 中使用以下内容:
int main(void)
{
CURL *easyhandle;
curl_global_init(CURL_GLOBAL_ALL);
easyhandle = curl_easy_init();
if(easyhandle) {
char *data="json={xxx:200}&apikey=xxxxx";
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(easyhandle, CURLOPT_URL, "http://localhost/xxxx/input/post.json");
curl_easy_perform(easyhandle);
curl_easy_cleanup (easyhandle);
}
curl_global_cleanup();
return 0;
}
这实际上是我想要实现的目标:
http://localhost/xxxx/input/post.json?json={xxx:200}&apikey=xxxxx
它似乎不起作用。 :( 我是卷曲的新手。请帮忙。
谢谢!
【问题讨论】:
-
JSON 数据在发送前需要进行编码。我不记得这样做的 C 函数,但有一个谷歌。
-
@ScottMcGready 谢谢!我会调查的。只是想知道 bash 命令是否也有问题。
-
好吧,试一下 HTML 实体的编码,然后看看错误是什么。必须先走路才能跑;)
-
@ScottMcGready 但无论如何在 GET 请求中发送 JSON 可能是个坏主意。