【问题标题】:curl request through c++ for indexing solr documentcurl请求通过c ++索引solr文档
【发布时间】:2016-05-17 06:52:55
【问题描述】:

我使用以下代码在 solr 中索引文档

CURL *curl = curl_easy_init(); 
CURLcode res;
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
    data. */
    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.164:8983/solr/collection1/update?replacefields=false  -H 'Content-type: application/json' -d '[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown \", \"subject\":\"Life\"}]'");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
     if(CURLE_OK == res){
        logger.LogError("res value CURLE_OK");
    }
    /* always cleanup */
    curl_easy_cleanup(curl);
}

curl_easy_perform(curl) 的返回值,即 res 是 CURLE_OK,但记录没有在 solr 的 collection1 中建立索引,并且在从终端记录发布以下命令时被索引

curl http://192.168.0.164:8983/solr/collection1/update?replacefields=false  -H 'Content-type: application/json' -d '[{"id":"4000", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'

【问题讨论】:

    标签: c++ curl solr


    【解决方案1】:

    您没有为 cURL 提供正确的 URL。实际 URL -H 'Content-type: application/json' -d '[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown \", \"subject\":\"Life\"}]' 之后的所有内容只是 cURL 命令行工具的参数,而不是 URL 的一部分。

    URL 选项只能是http://192.168.0.164:8983/solr/collection1/update?replacefields=false。其余参数必须设置为它们自己的curl_easy_setopt 调用。

    要将数据设置为 POST,请使用 CURLOPT_POSTFIELDS

    传递一个 char * 作为参数,指向要在 HTTP POST 操作中发送的完整数据。您必须确保按照您希望服务器接收数据的方式格式化数据。

    要设置正确的请求内容类型,请使用CURLOPT_HTTPHEADER

    将指向 HTTP 标头链接列表的指针传递给 HTTP 请求中的服务器和/或代理。主机请求和代理请求都可以使用同一个列表!

    设置所有选项后调用curl_easy_perform。您可能还想查看 Solr 服务器上的日志以查看 Solr 是否生成异常。您还可以设置 CURLOPT_ERRORBUFFERCURLOPT_VERBOSE 以获取有关 cURL 内部任何故障的更多信息(只要 cURL 能够发出请求,就会返回 CURLE_OK - 但如果服务器返回 400 或 @ 则不会更改987654331@ 或服务器端的任何实际错误代码(除非设置了CURLOPT_FAILONERROR)。

    【讨论】:

    • 嗨已经尝试过上述建议,但在终端上我收到错误消息:4005缺少内容流400
    • 我认为我没有正确提供参数。我应该如何传递 -H 'Content-type: application/json' -d 参数
    • @Anuja 这就是我帖子的内容。使用CURLOPT_POSTFIELDS 将数据设置为POST(-d 参数)并使用CURLOPT_HTTPHEADER 设置Content-Type:-header。
    • 我曾使用 CURLOPT_POSTFIELDS 传递 json 字符串([{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc \":\"unknown \", \"subject\":\"Life\"}]) 能否提供curl的确切参数序列和值来处理solr中的数据
    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2011-05-27
    • 2012-08-23
    相关资源
    最近更新 更多