How to query with curl?

I’m running the latest versions of CouchBase and N1QL (as of now Community 2.2.0-837-rel and Developer Preview 3 respectively) and I’m able to do queries using the cbq command, but I’d like to do them with curl.

I tried curl localhost:8093/query -d ‘select * from free’ -v but I get a 500 error and a body of Missing required query string.

I looked at https://github.com/couchbaselabs/query/blob/master/shell/shell.go#L34 and from there I got that it should be a POST request to /query of the cbq-engine server, with the N1QL query string on the request body.

Then I started a nc server and pointed the cbq command to there, just to see what request it was making, and this is what I’m getting:

► nc -l 9090 POST /query HTTP/1.1 Host: localhost:9090 User-Agent: Go 1.1 package http Content-Length: 37 Content-Type: text/plain Accept-Encoding: gzip

select * from free where _oid = “pp”;

So what am I missing ? Do the User-Agent and Accept-Encoding make the difference ?

Update: Is it coming from here: https://github.com/couchbaselabs/tuqtng/blob/master/network/http/http_query.go#L36 ?

Try

curl -d ‘select * from free’ -v http://localhost:8093/query

that’s the same exact command I used before, copied and pasted yours in case I had missed something, but got the same result, status code 500 and Missing required query string.

There might be something specific to your version of curl.

Try the -F and --form-string options with the q=… parameter.

Also, note that your initial posting had the -d option at the end of the curl command, which would be incorrect.

1 Like

yep, that was it :slight_smile: these:

curl -F 'q=select * from free' http://localhost:8093/query curl -d 'q=select * from free' http://localhost:8093/query

both worked :slight_smile:

Thanks!