c - 无法使用 libcurl 将 HTTP PUT 发送到 django-piston

标签 c django http curl django-piston

我正在尝试使用 libcurl 来模拟命令 PUT 数据

curl -u test:test -X PUT --data-binary @data.yaml "http://127.0.0.1:8000/foo/"

它工作正常。我的选项如下所示:

curl_easy_setopt(handle, CURLOPT_USERPWD, "test:test");
curl_easy_setopt(handle, CURLOPT_URL, "http://127.0.0.1:8000/foo/");
curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_data);
curl_easy_setopt(handle, CURLOPT_READDATA, &yaml);
curl_easy_setopt(handle, CURLOPT_INFILESIZE, yaml.size());
curl_easy_perform(handle);

我相信 read_data 函数可以正常工作,但如果您询问,我会发布该代码。

我正在使用 Django django-piston ,并且我的 update 函数从未被调用! (当我使用上面的命令行版本时,会调用它。)

libcurl 的输出是:

* About to connect() to 127.0.0.1 port 8000 (#0)
*   Trying 127.0.0.1... * connected
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
* Server auth using Basic with user 'test'
> PUT /foo/ HTTP/1.1
Authorization: Basic dGVzdDp0ZXN0
Host: 127.0.0.1:8000
Accept: */*
Content-Length: 244
Expect: 100-continue

* Done waiting for 100-continue
** this is where my read_data handler confirms: read 244 bytes **
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Date: Thu, 13 May 2010 08:22:52 GMT
< Server: WSGIServer/0.1 Python/2.5.1
< Vary: Authorization
< Content-Type: text/plain
< 
Bad Request* Closing connection #0

最佳答案

我没有给出正确的标题。当使用 -v 运行命令行版本时,它给出了标题

Content-Type: application/x-www-form-urlencoded

要使用 libcurl 添加此内容:

struct curl_slist *slist = 0;
slist = curl_slist_append(slist, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
// ...

curl_easy_perform(handle);

curl_slist_free_all(slist);

它有效!

关于c - 无法使用 libcurl 将 HTTP PUT 发送到 django-piston,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2825372/

相关文章:

c - getopt 无法识别 c 中的多个命令行标志

c - Mac OS 中的 ranlib/ar 给我 "has no symbols"警告

python - DjangoassertTemplateUsed 重定向后失败

javascript - Promise 在解决之前不等待嵌套 promise

c# - 在 C# 中临时加载用于客户端身份验证的 SSL 客户端 key

node.js - 限制事件驱动的 Nodejs HTTP 请求

c - 裸机中 zynq 中的以太网驱动程序实现

c - 为什么程序在给定展开因子 k>C*L 的情况下达到吞吐量界限?

python - 以编程方式更新 Wagtail 中的编辑界面

python - 用于插入或替换 URL 参数的 Django 模板标记