简体   繁体   English

C LibCurl文件上传到Rails服务器上的ruby

[英]C LibCurl file upload to ruby on rails server

I am trying to upload text files to a ruby on rails server. 我正在尝试将文本文件上传到Rails服务器上的ruby。 I was successful at uploading the file through CLI: 我通过CLI成功上传了文件:

curl -X POST --data-urlencode data@/etc/filepath/ www.example.com/path

Now, trying with libCurl through C: 现在,通过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);
    }

Any ideas why running through CLI would work, but libCurl does not? 有什么想法可以通过CLI运行,但是libCurl不行吗? Am I missing some curl options or does my server require alternate configuration? 我是否缺少某些curl选项,或者我的服务器需要备用配置?

Thanks 谢谢

You want CURLOPT_POSTFIELDS or CURLOPT_UPLOAD basically. 您基本上需要CURLOPT_POSTFIELDS CURLOPT_UPLOAD Do you want to provide the data as a string or read it from the file? 您要以字符串形式提供数据还是从文件中读取数据?

You also don't want both CURLOPT_POST and CURLOPT_UPLOAD , as the later implies PUT . 您也不希望同时使用 CURLOPT_POSTCURLOPT_UPLOAD ,因为后者暗示了PUT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM