使用POST的curl_easy_perform等待EOF

时间:2017-01-26 20:30:47

标签: c++ curl libcurl eof

我正在尝试使用curl_easy_perform()进行HTTP POST请求,但是当代码到达时

res = curl_easy_perform(curl);

它等待EOF。当我按下Ctrl + D时,代码成功完成。我正在使用XCode IDE。即使程序从终端运行为已编译的可执行文件,我也遇到同样的问题。

这是我的带有卷曲设置的POST功能:

int BitfinexAPI::
DoPOSTrequest(const string &UrlEndPoint, const string &params, string &result)
{

    if(curl) {

        string url = APIurl + UrlEndPoint;
        string payload;
        string signature;
        getBase64(params, payload);
        getHmacSha384(secretKey, payload, signature);

        // Headers
        struct curl_slist *httpHeaders = NULL;
        httpHeaders = curl_slist_append(httpHeaders, ("X-BFX-APIKEY:" + accessKey).c_str());
        httpHeaders = curl_slist_append(httpHeaders, ("X-BFX-PAYLOAD:" + payload).c_str());
        httpHeaders = curl_slist_append(httpHeaders, ("X-BFX-SIGNATURE:" + signature).c_str());
        httpHeaders = curl_slist_append(httpHeaders, ("Expect:"));

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); // debug option
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
        res = curl_easy_perform(curl);

        // libcurl internal error handling
        if (res != CURLE_OK)
        {
            cout << "Libcurl error in DoPOSTRequest(), code:\n";
            return res;
        }
        return res;

    }
    else
    {
        // curl not properly initialized curl = NULL
        return curlERR;

    }
}

这是在按下Ctrl-D后从CURLOPT_VERBOSE,0L输出的 之后&lt; HTTP / 1.1 100继续行:

*   Trying 104.16.175.181...
* TCP_NODELAY set
* Connected to api.bitfinex.com (104.16.175.181) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: ssl453718.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
> POST /v1/account_infos/ HTTP/1.1
Host: api.bitfinex.com
Accept: */*
X-BFX-APIKEY:NKc8RCJUDPfp1R8enTPSAi0soozYFn9MezjzmnFQJ3X
X-BFX-PAYLOAD:eyJyZXF1ZXN0IjoiL3YxL2FjY291bnRfaW5mb3MiLCJub25jZSI6IjE0ODU0NjIyNDUzNzE2MTYifQ==
X-BFX-SIGNATURE:95c396c792b59b8084b9f2523b6b7c0e948ae555e2677a07fd42e54050360a44f8f0e184db5b9fa63c4635816180ba1e
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Thu, 26 Jan 2017 20:24:05 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d3d747333090623a13f70c430103686ed1485462245; expires=Fri, 26-Jan-18 20:24:05 GMT; path=/; domain=.bitfinex.com; HttpOnly
< Strict-Transport-Security: max-age=31536000
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Vary: Accept-Encoding
< ETag: W/"0ac5694d4f0d0485ba2e4640ac79cc40"
< Cache-Control: no-cache
< X-Request-Id: b5f58f5f-473f-428b-a889-74c2e7b43278
< X-Runtime: 0.029113
< Expires: Thu, 26 Jan 2017 20:24:04 GMT
< X-Frame-Options: SAMEORIGIN
< Server: cloudflare-nginx
< CF-RAY: 3276afba2efd3e20-PRG
< 
* Curl_http_done: called premature == 0
* Connection #0 to host api.bitfinex.com left intact
[{"maker_fees":"0.1","taker_fees":"0.2","fees":[{"pairs":"BTC","maker_fees":"0.1","taker_fees":"0.2"},{"pairs":"LTC","maker_fees":"0.1","taker_fees":"0.2"},{"pairs":"ETH","maker_fees":"0.1","taker_fees":"0.2"},{"pairs":"ETC","maker_fees":"0.1","taker_fees":"0.2"},{"pairs":"ZEC","maker_fees":"0.1","taker_fees":"0.2"},{"pairs":"XMR","maker_fees":"0.1","taker_fees":"0.2"}]}]
0
Program ended with exit code: 0

1 个答案:

答案 0 :(得分:1)

问题:代码集CURLOPT_POST但它似乎无法指示任何方式 libcurl发送该POST请求中的任何数据?这将使libcurl使用默认的内部读取函数,该函数从stdin ...

读取

修复:使用CURLOPT_POSTFIELDS提供POST数据,或者如果您愿意,可以使用CURLOPT_READFUNCTION设置阅读回调。