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

发布于 2024-09-01 09:05:05 字数 1559 浏览 4 评论 0原文

命令

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

我正在尝试使用 libcurl 来 PUT 数据来模仿正常工作的 。我的选项如下所示:

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 一起使用,以及我的 更新 函数永远不会被调用! (当我使用上面的命令行版本时,会调用它。)

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

I'm trying to PUT data using libcurl to mimic the command

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

which works correctly. My options look like:

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);

I believe the read_data function works correctly, but if you ask, I'll post that code.

I'm using Django with django-piston, and my update function is never called! (It is called when I use the command line version above.)

libcurl's output is:

* 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-09-08 09:05:05

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

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

To add this with 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);

并且它有效!

I wasn't giving the proper header. When running the command line version with -v, it gave the header

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

To add this with 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);

and it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文