newbie question - using CURL

28 views
Skip to first unread message

will

unread,
Aug 17, 2010, 9:47:13 AM8/17/10
to CouchApp
Hello all,

This is really basic I think, though for myself I want to work through
the fundamentals so I get a good feel for how CouchDB works -- At
least to the crawling/toddler stage.

I want to use the simple PUT example from the "CouchDB Book", "Core
API" section, viz.

http://books.couchdb.org/relax/intro/core-api

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af
-d '{"title":"There is Nothing Left to Lose","artist":"Foo
Fighters"}'

When I run this example an error is returned from CouchDB about the
JSON formatting. (And I did get a 'fresh' UUID, not just the one in
the example).

{"error":"bad_request","reason":"invalid UTF-8 JSON"}

My interpretation is that there's something 'missing' in the way the
JSON is sent (via cURL command line). I have pasted the full HTTP
response below from cURL/CouchDB.

I'm running on a Windows system, so I wonder if the problem is with
the quoting (I tried a couple of //common// variations -- No luck).

The other thing, is there a way to 'force' a

application/json Content-Type

In the cURL command?

While I realise I don't need to do this to 'get into' couchDB -- I
would like a simple way to learn, try prototypes, and simple tests
using something LIKE cURL, so any helpful answers or things to try are
most welcome.

Thanks in advance.

... Will

----------------

full response:

> PUT /albums/f9517b553c87bd5e08ccd4fcef001463 HTTP/1.1
> User-Agent: curl/7.16.4 (i686-pc-mingw32) libcurl/7.16.4 zlib/1.2.3
> Host: 127.0.0.1:5984
> Accept: */*
> Content-Length: 57
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 400 Bad Request
< Server: CouchDB/0.11.0 (Erlang OTP/R13B)
< Date: Tue, 17 Aug 2010 13:15:55 GMT
< Content-Type: text/plain;charset=utf-8
< Content-Length: 54
< Cache-Control: must-revalidate
<
{"error":"bad_request","reason":"invalid UTF-8 JSON"}
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

Benoit Chesneau

unread,
Aug 17, 2010, 10:00:36 AM8/17/10
to couc...@googlegroups.com

You indeed need to pass the Content-Type.

- benoit

Nils Breunese

unread,
Aug 17, 2010, 10:20:06 AM8/17/10
to couc...@googlegroups.com
will wrote:

> The other thing, is there a way to 'force' a
>
> application/json Content-Type
>
> In the cURL command?

You can use the -H option to pass extra headers. This can be found in the output of 'curl --help' and also in the curl man page.

> While I realise I don't need to do this to 'get into' couchDB -- I
> would like a simple way to learn, try prototypes, and simple tests
> using something LIKE cURL, so any helpful answers or things to try are
> most welcome.

For OS X I sometimes use HTTP Client to interact with HTTP servers: http://ditchnet.org/httpclient/

Google for REST client or HTTP client and you might find some interesting tools.

If you're into a particular scripting or programming language you could also dig into a good library for that platform. IMO couchdbkit is a great toolkit for Python for instance.

Nils.

De informatie vervat in deze e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Jason Smith

unread,
Aug 17, 2010, 10:25:11 AM8/17/10
to couc...@googlegroups.com
On Tue, Aug 17, 2010 at 20:47, will <william....@gmail.com> wrote:
Hello all,

This is really basic I think, though for myself I want to work through
the fundamentals so I get a good feel for how CouchDB works -- At
least to the crawling/toddler stage.

I want to use the simple PUT example from the "CouchDB Book", "Core
API" section, viz.

  http://books.couchdb.org/relax/intro/core-api

    curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af
           -d '{"title":"There is Nothing Left to Lose","artist":"Foo
Fighters"}'

It looks like you sent this email using Firefox for Windows. At the standard Windows terminal, the single-quotes from the book examples do not work.

Instead you must use double quotes. And then inside those quotes, you have to use backslash-doublequote.

    curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af -d "{\"title\":\"There is Nothing Left to Lose\", \"artist\":\"Foo Fighters\"}"

--
Jason Smith
Couchio Hosting

will

unread,
Aug 18, 2010, 8:07:37 AM8/18/10
to CouchApp
Thanks Jason!

That worked a treat ... And thanks to you others, during the day I was
thinking a small Ruby or Python interactive might be the go not too
far down the track.

:-)

On Aug 18, 12:25 am, Jason Smith <j...@couch.io> wrote:
> On Tue, Aug 17, 2010 at 20:47, will <william.full.m...@gmail.com> wrote:
>
> > I want to use the simple PUT example from the "CouchDB Book", "Core
> > API" section, viz.
>
> > http://books.couchdb.org/relax/intro/core-api
>
> Instead you must use double quotes. And then inside those quotes, you have
> to use backslash-doublequote.
>
> curl -X PUThttp://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af
> -d "{\"title\":\"There is Nothing Left to Lose\", \"artist\":\"Foo Fighters\"}"

will

unread,
Aug 28, 2010, 6:54:51 AM8/28/10
to CouchApp
G'day again,

I hope everyone is well. I have a follow-up question from the (new
Draft, now) of the "CouchDB Book"

* http://guide.couchdb.org/draft/index.html
-- core api: http://guide.couchdb.org/draft/api.html

There's a simple example to attach a JPEG to a couch document (guid-s
matching my db):

curl -vX
PUT http://127.0.0.1:5984/albums/70b50bfa0a4b3aed1f8aff9e92dc16a0/
artwork.jpg?rev=1-d789a27476ed3962a24736e4138ff71f
--data-binary @artwork.jpg -H "Content-Type: image/jpg"

I am using cURL from a Windows cmd line shell, yes that was correct
(Windows/XP).

Given the issue with quotes(") last time, I tried a few obvious
permutations with "\"@artwork.jpg\"" and others. There is a file
artwork.jpg in my current directory.

I get an error from the server:

-- * Could not resolve host: artwork.jpg; Host not found

I also tried full path names and such.

I'm stumped now. Any ideas?

Thanks in advance,

Will.


#############
cURL output

* About to connect() to 127.0.0.1 port 5984 (#0)
* Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 5984 (#0)
> PUT /albums/70b50bfa0a4b3aed1f8aff9e92dc16a0/ HTTP/1.1
> User-Agent: curl/7.16.4 (i686-pc-mingw32) libcurl/7.16.4 zlib/1.2.3
> Host: 127.0.0.1:5984
> Accept: */*
> "Content-Type: image/jpg"
> Content-Length: 14
> Content-Type: application/x-www-form-urlencoded
>
* Empty reply from server
* Connection #0 to host 127.0.0.1 left intact
curl: (52) Empty reply from server
* Could not resolve host: artwork.jpg; Host not found
* Closing connection #1
curl: (6) Could not resolve host: artwork.jpg; Host not found
* Closing connection #0

#############


On Aug 18, 12:25 am, Jason Smith <j...@couch.io> wrote:
> On Tue, Aug 17, 2010 at 20:47, will <william.full.m...@gmail.com> wrote:
>
> > This is really basic I think, though for myself I want to work through
> > the fundamentals so I get a good feel for how CouchDB works -- At
> > least to the crawling/toddler stage.
>
> > I want to use the simple PUT example from the "CouchDB Book", "Core
> > API" section, viz.
>
> >  http://books.couchdb.org/relax/intro/core-api
>
> >     curl -X PUT
> >http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af
> >            -d '{"title":"There is Nothing Left to Lose","artist":"Foo
> > Fighters"}'
>
> It looks like you sent this email using Firefox for Windows. At the standard
> Windows terminal, the single-quotes from the book examples do not work.
>
> Instead you must use double quotes. And then inside those quotes, you have
> to use backslash-doublequote.
>
>     curl -X PUThttp://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af-d
> "{\"title\":\"There is Nothing Left to Lose\", \"artist\":\"Foo Fighters\"}"
Reply all
Reply to author
Forward
0 new messages