Eric Mitjans
Eric Mitjans

Reputation: 2169

Bad request with Cloudant, PHP and range query

I'm trying to get a range query to work with PHP cURL and Cloudant.

I've setup the following search index in my Cloudant:

{
  "_id": "_design/date",
  "_rev": "20-822ebda694f4fb57dc0a48bf60f6570f",
  "views": {},
  "language": "javascript",
  "indexes": {
    "by_date": {
      "analyzer": "classic",
      "index": "function (doc) {\n  index(\"default\", doc.datequery);\n}"
    }
  }
}

And then in my PHP document I do the following:

$urlstuff = "[20180110 TO 20180119]";

$url = "https://user:[email protected]/db/_design/date/_search/by_date?q=".$urlstuff."&include_docs=true&limit=200";
$ch = curl_init();   // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);

And I get Bad Request.

If I replace:

$urlstuff = "[20180110 TO 20180119]"; 

for

$urlstuff = "20180117";

Then I get good results.

Is it the square brackets messing with my cURL? What am I missing?

Upvotes: 0

Views: 110

Answers (1)

ptitzler
ptitzler

Reputation: 923

Yes, you are on the right track. If you escape your variable (e.g. using http://php.net/manual/en/function.curl-escape.php?) your query should work:

curl -g -X GET   'https://user:[email protected]/db/_design/date/_search/by_date?q=[20180110 TO 20180119]&include_docs=true&limit=20'   


{"error":"bad_request","reason":"Bad request"}

vs

curl -X GET 'https://user:[email protected]/db/_design/date/_search/by_date?q=%5B20180110%20TO%2020180119%5D&include_docs=true&limit=20' 

{"total_rows":1, ...

Upvotes: 2

Related Questions