C LibCurl 文件上传到 rails 服务器上的 ruby
C LibCurl file upload to ruby on rails server
我正在尝试将文本文件上传到 rails 服务器上的 ruby。我成功通过 CLI 上传了文件:
curl -X POST --data-urlencode data@/etc/filepath/ www.example.com/path
现在,尝试通过 C:
使用 libCurl
FILE *fd;
fd = fopen(file_path, "rb");
if (!fd)
return -1;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
}
知道为什么 运行 通过 CLI 可以工作,但 libCurl 不行吗?我是否缺少某些 curl 选项,或者我的服务器是否需要备用配置?
谢谢
基本上你想要CURLOPT_POSTFIELDS
或CURLOPT_UPLOAD
。您要以字符串形式提供数据还是从文件中读取数据?
你也不想要 both CURLOPT_POST
和 CURLOPT_UPLOAD
,因为后者暗示 PUT
.
我正在尝试将文本文件上传到 rails 服务器上的 ruby。我成功通过 CLI 上传了文件:
curl -X POST --data-urlencode data@/etc/filepath/ www.example.com/path
现在,尝试通过 C:
使用 libCurl FILE *fd;
fd = fopen(file_path, "rb");
if (!fd)
return -1;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
}
知道为什么 运行 通过 CLI 可以工作,但 libCurl 不行吗?我是否缺少某些 curl 选项,或者我的服务器是否需要备用配置?
谢谢
基本上你想要CURLOPT_POSTFIELDS
或CURLOPT_UPLOAD
。您要以字符串形式提供数据还是从文件中读取数据?
你也不想要 both CURLOPT_POST
和 CURLOPT_UPLOAD
,因为后者暗示 PUT
.