【问题标题】:Unable to HTTP PUT with libcurl to django-piston无法使用 libcurl HTTP PUT 到 django-piston
【发布时间】:2010-05-13 08:38:30
【问题描述】:

我正在尝试使用 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-piston 的Django,而我的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

【问题讨论】:

    标签: c django http curl django-piston


    【解决方案1】:

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

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

    它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      相关资源
      最近更新 更多